gpt4 book ai didi

docker - 基于 busybox 将 Linux 实用程序添加到 docker 镜像

转载 作者:行者123 更新时间:2023-12-02 18:48:18 25 4
gpt4 key购买 nike

当我为 busybox 容器尝试 df -h 时,我得到以下结果:

$ docker run -it busybox du -h  
# expected results

我需要的是 df -b 的输出,它给我以下内容:

$ docker run -it busybox du -b                                                                     
du: invalid option -- b
BusyBox v1.30.0 (2018-12-31 18:16:17 UTC) multi-call binary.

Usage: du [-aHLdclsxhmk] [FILE]...

Summarize disk space used for each FILE and/or directory

-a Show file sizes too
-L Follow all symlinks
-H Follow symlinks on command line
-d N Limit output to directories (and files with -a) of depth < N
-c Show grand total
-l Count sizes many times if hard linked
-s Display only a total for each argument
-x Skip directories on different filesystems
-h Sizes in human readable format (e.g., 1K 243M 2G)
-m Sizes in megabytes
-k Sizes in kilobytes (default)

由于许多标准实用程序在 busybox 图像中被修剪或不存在,因此这种行为并不奇怪。作为 dockerhub 的 busybox 页面 suggests :

FROM busybox
COPY ./my-static-binary /my-static-binary
CMD ["/my-static-binary"]

因此,我创建了一个包含以下内容的 Dockerfile,试图将我的 Ubuntu 16.04 du 二进制文件复制到图像中:

FROM busybox
COPY /usr/bin/du /bin/du
CMD ["/bin/du", "-b"]

但是当我尝试 docker build 时,出现以下错误:

$ docker build .              
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
---> 3a093384ac30
Step 2/3 : COPY /usr/bin/du /bin/du
COPY failed: stat /var/lib/docker/tmp/docker-builder362173879/usr/bin/du: no such file or directory

我不知道将实用程序添加到此类最小图像是否是正确的方法,但如果您让我知道实用程序(例如 (complete) ducurl 等可以添加,因为没有像 apt 这样的包管理器。

最佳答案

如果只有 Busybox 的 Docker 基础镜像不能满足您的需求,您可以更改 Dockerfile 以基于功能更全的 Linux 发行版。 FROM ubuntu 非常常见,包括 Unix 工具集的 GNU 版本(及其各种供应商扩展); FROM alpine 也很常见,它基于 Busybox 加上一个最小的包管理器。

另一个好的答案是限制自己使用 POSIX.1 中定义的功能: du(1) is not required to support a -b option .如果您尝试编写基于 Alpine 的图像或在非 Linux 系统上运行(MacOS 是当今最突出的例子),这将有所帮助。

您可能无法成功地将单个二进制文件从您的主机系统复制到 Docker 镜像中,路径问题除外,因为库环境可能非常不同。如果您在主机上运行 ldd $(which du),则那里列出的所有库都需要以相似的版本出现在镜像中。 busybox 基础镜像可能甚至不包含 libc.so.6,这是大多数动态链接二进制文件的最低要求。

问题的正确答案是编写一个多阶段 Dockerfile,该文件的第一阶段带有完整的 C 工具链,用于构建 GNU Coreutils 的静态版本。 ,然后是第二阶段将其复制进去。对于一个可能不是您实际想要运行的核心应用程序的一部分的工具来说,这是大量工作。

关于docker - 基于 busybox 将 Linux 实用程序添加到 docker 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54103839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com