Docker 在容器内的 PATH 中没有看到二进制文件

Docker doesn't see a binary in PATH inside container

我正在尝试构建一个 Docker 图像以帮助从 Google Protocol Buffers 规范文件自动生成代码。

我当前的Docker文件:

FROM alpine:latest

WORKDIR /protobuf
VOLUME /dst

RUN apk update \
 && apk add --no-cache bash \
 && apk add --virtual .build-deps curl unzip \
 && curl -k -L https://github.com/google/protobuf/releases/download/v3.6.0/protoc-3.6.0-linux-x86_64.zip -o protoc.zip \
 && unzip protoc.zip \
 && rm protoc.zip \
 && chmod +x ./bin/protoc \
 && apk --purge del .build-deps

COPY . spec/
RUN chmod +x spec/entrypoint.sh

ENV SRC_DIR=/usr/src/protoc/spec DST_DIR=/dst PATH=/protobuf/bin:${PATH}
ENTRYPOINT [ "/bin/bash" ]

当我在启用 TTY 的情况下构建并 运行 它时,它 运行 没有问题。但是,容器内的 shell 无法看到 /protobuf/bin/protoc 二进制文件,即使它应该能够:

bash-4.4# echo $PATH
/protobuf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
bash-4.4# ls -l /protobuf/bin
total 4444
-rwxr-xr-x    1 root     root       4548312 Jun 15 23:40 protoc
bash-4.4# protoc
bash: /protobuf/bin/protoc: No such file or directory

这可能是什么问题?

protobuf 链接到 glibc library and alpine uses musl (and older alpine used uclibc). They are not compatible. The error you are seeing comes from the linker not finding the libc standard library /lib64/ld-linux-x86-64.so.2. You can check that, by issuing ldd 命令:

$ ldd /bin/protoc 
    /lib64/ld-linux-x86-64.so.2 (0x7faa1a641000)
    libm.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7faa1a641000)
    libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7faa1a641000)
    libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7faa1a641000)
Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /bin/protoc)

文件存在,PATH没问题
要解决这个问题,你可以在 alpine 上安装 glibc(不推荐)或者只是移动到一个普通的 linux 容器。

@edit:我的旧答案是不正确的,bash 和 sh 默认情况下都不会来源 /etc/profile,只有在提供 -l 选项时才会来源。