Docker 带有 Alpine 的图像 Linux: 肯定有一个可执行文件,但在尝试执行时找不到

Docker image with Alpine Linux: an executable file is definitely there but cannot be found while trying to execute

这是我的终端的输出,当时我在一个带有 Alpine 图像的 Docker 容器的 shell 中:

bash-5.0# ls
makeThumb
bash-5.0# ./makeThumb 
bash: ./makeThumb: No such file or directory
bash-5.0# 

如您所见,我有一个名为 makeThumb 的可执行文件,它确实存在(请参阅 ls 的输出)。然而,奇怪的是,当我试图用 ./makeThumb 执行它时,它说 No such file or directory.

如何解决?

我的Docker文件:

FROM mhart/alpine-node:14 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .

FROM mhart/alpine-node:14
RUN apk update && apk add bash
COPY --from=build /app/ /app/
WORKDIR /app
RUN npm prune --production
EXPOSE 3000

CMD [ "node", "server.js" ]

ldd makeThumb 的输出:

ldd makeThumb 
    /lib64/ld-linux-x86-64.so.2 (0x7f0c421ae000)
    libdl.so.2 => /lib64/ld-linux-x86-64.so.2 (0x7f0c421ae000)
    libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f0c421ae000)
    librt.so.1 => /lib64/ld-linux-x86-64.so.2 (0x7f0c421ae000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7f0c42015000)
    libm.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f0c421ae000)
    libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x7f0c42001000)
    libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f0c421ae000)
Error relocating makeThumb: __strdup: symbol not found
Error relocating makeThumb: __vfprintf_chk: symbol not found
Error relocating makeThumb: __sprintf_chk: symbol not found
Error relocating makeThumb: __snprintf_chk: symbol not found
Error relocating makeThumb: __vsnprintf_chk: symbol not found
Error relocating makeThumb: __strcat_chk: symbol not found
Error relocating makeThumb: __memset_chk: symbol not found
Error relocating makeThumb: __fprintf_chk: symbol not found
Error relocating makeThumb: __memcpy_chk: symbol not found
Error relocating makeThumb: __longjmp_chk: symbol not found

Alpine 使用 musl 这是一个极简的 libc。我的猜测是您的二进制文件使用的是 Alpine 下不存在的非标准函数。

我看到了两个解决方案:

  • 尝试在您的 docker 容器中安装 glibc
  • 可能是最简单的解决方案:尝试找到将其用作默认图像的 Docker 图像(简约的 Debian/Ubuntu Docker 应该可以做到)