Adding Linux utilities to docker image based on busybox

Far*_*igo 2 busybox docker dockerfile

When I try df -h for busybox container I get the following results:

$ docker run -it busybox du -h  
# expected results
Run Code Online (Sandbox Code Playgroud)

What I need is the output of df -b which gives me the following:

$ 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)
Run Code Online (Sandbox Code Playgroud)

As many of the standard utilites are trimmed in busybox image or non-existent, the behaviour is not surprising. As dockerhub's busybox page suggests:

FROM busybox
COPY ./my-static-binary /my-static-binary
CMD ["/my-static-binary"]
Run Code Online (Sandbox Code Playgroud)

So, I created a Dockerfile with the following content trying to copy my Ubuntu 16.04 du binary to the image:

FROM busybox
COPY /usr/bin/du /bin/du
CMD ["/bin/du", "-b"]
Run Code Online (Sandbox Code Playgroud)

But when I try docker build I get the following error:

$ 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
Run Code Online (Sandbox Code Playgroud)

I don't know if it's the right way of adding utilities to such minimal images, but I would appreciate if you let me know the ways that utilities such as (complete) du, curl, etc. could be added given that there is no package manager like apt.

Dav*_*aze 5

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

另一个好的答案是将自己限制在POSIX.1中定义的功能:du(1) 不需要支持 -b 选项。如果您尝试编写基于 Alpine 的映像或在非 Linux 系统上运行(MacOS 是当今最突出的示例),这将有所帮助。

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

所写问题的正确答案是编写一个多阶段 Dockerfile,其中第一阶段包含完整的 C 工具链,用于构建静态版本的GNU Coreutils,然后是第二阶段,将其复制到其中。这是很多使用的工具可能不是您实际想要运行的核心应用程序的一部分。