当前位置: 首页> 房产> 家装 > 把本机的bash构建到docker镜像里面

把本机的bash构建到docker镜像里面

时间:2025/7/11 1:10:56来源:https://blog.csdn.net/qq_33906471/article/details/139700349 浏览次数:1次

最近突发奇想,想把本机的bash放到docker镜像里面,接下来看操作。

获取bash以及依赖

[root@bogon ~]# cat get_lib_info.sh 
#!/bin/bash# 函数:显示帮助信息
show_help() {echo "Usage: $(basename "$0") -h -f <file> -b <output_directory>"echo "  -h          Display this help message"echo "  -f <file>   Specify the binary file to query"echo "  -b <output_directory>  Specify the output directory for the compressed file"
}# 函数:使用 ldd 和 awk 提取库文件路径
extract_library_paths() {local binary_path="$1"local paths=()# 运行 ldd 命令,过滤并提取库文件路径while IFS= read -r line; doif [[ "$line" =~ ^/ ]]; thenpaths+=("$line")fidone < <(ldd "$binary_path" | awk '/=>/ {if ($3 ~ /^\//) print $3}')while IFS= read -r line; doif [[ "$line" =~ ^/ ]]; thenpaths+=("$line")fidone < <(ldd "$binary_path" | awk '{print $1}' | grep '^/')# 返回路径数组echo "${paths[@]}"
}# 函数:解析符号链接路径
resolve_symlinks() {local path="$1"# 处理可能存在的符号链接链while [[ -L "$path" ]]; dotarget=$(readlink "$path")if [[ "${target:0:1}" != "/" ]]; thentarget=$(dirname "$path")/"$target"fipath="$target"done# 输出最终的真实路径echo "$path"
}# 主函数:处理二进制文件并生成解析后的路径
process_binary() {local binary=""local output_directory="$(dirname "$0")"# 如果没有提供任何参数,则显示帮助信息if [ "$#" -eq 0 ]; thenshow_helpexit 0fi# 解析命令行参数while getopts "hf:b:" opt; docase "$opt" inh)  # 显示帮助信息show_helpexit 0;;f)  # 指定要查询的文件binary="$OPTARG"if [ ! -f "$binary" ]; thenecho "Error: File '$binary' does not exist." >&2exit 1fi;;b)  # 指定输出的目录output_directory="${OPTARG}";;*)  # 处理未知选项echo "Invalid option: -$OPTARG" >&2show_helpexit 1;;esacdone# 检查是否提供了二进制文件路径if [ -z "$binary" ]; thenecho "Error: You must specify a binary file with the -f option." >&2show_helpexit 1fi# 提取库文件路径library_paths=($(extract_library_paths "$binary"))# 解析符号链接路径resolved_paths=()for path in "${library_paths[@]}"; doresolved_path=$(resolve_symlinks "$path")resolved_paths+=("$resolved_path")donefor path in "${library_paths[@]}"; doresolved_paths+=("$path")done# 打印解析后的路径# for path in "${resolved_paths[@]}"; do#     echo "$path"# done# 创建解析后路径的 tar 压缩包local binary_name=$(basename "$binary")tar_filename="${output_directory}/${binary_name##*/}.tar.gz"tar zcvf "$tar_filename" "${resolved_paths[@]}"echo "已创建压缩包:$tar_filename"
}# 主程序开始
process_binary "$@"[root@bogon ~]# chmod +x get_lib_info.sh 
[root@bogon ~]# ./get_lib_info.sh -f /bin/bash
tar: Removing leading `/' from member names
/lib64/libtinfo.so.5.9
/lib64/libdl-2.17.so
/lib64/libc-2.17.so
/lib64/ld-2.17.so
/lib64/libtinfo.so.5
/lib64/libdl.so.2
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
已创建压缩包:./bash.tar.gz[root@bogon ~]# cp /bin/bash ./

编写Dockerfile

[root@bogon ~]# cat Dockerfile 
FROM scratchADD bash.tar.gz /
COPY bash /bin/bashCMD ["/bin/bash"][root@bogon ~]# docker build -t mybash .
[+] Building 0.1s (6/6) FINISHED                                     docker:default=> [internal] load build definition from Dockerfile                           0.0s=> => transferring dockerfile: 108B                                           0.0s=> [internal] load .dockerignore                                              0.0s=> => transferring context: 2B                                                0.0s=> [internal] load build context                                              0.0s=> => transferring context: 1.02MB                                            0.0s=> [1/2] ADD bash.tar.gz /                                                    0.0s=> [2/2] COPY bash /bin/bash                                                  0.0s=> exporting to image                                                         0.0s=> => exporting layers                                                        0.0s=> => writing image sha256:86b7d3cc6ffbed8e9c7a921af3d6d671d4692cda8e4fa3924  0.0s=> => naming to docker.io/library/mybash                                      0.0s[root@bogon ~]# docker run -it --rm mybash
bash-4.2# exit
exit
[root@bogon ~]#

关键字:把本机的bash构建到docker镜像里面

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: