不能 运行 带有 Alpine 和 Busybox docker 图像的可执行文件

Cannot run executables with Alpine and Busybox docker images

我正在尝试创建一个非常简单的 docker 映像,它必须预先安装了 lua、luarocks 和其他一些可执行文件,docker文件如下:

FROM busybox AS builder

WORKDIR /usr/local/bin

# installs curl (reliable alternative to wget)
RUN wget -O curl https://github.com/moparisthebest/static-curl/releases/download/v7.79.1/curl-amd64 && \
    chmod +x curl

# installs luaformatter
RUN wget -O lua-format https://github.com/Koihik/vscode-lua-format/raw/master/bin/linux/lua-format && \
        chmod +x lua-format

# installs stylua
RUN wget -O stylua-0.11.0-linux.zip https://github.com/JohnnyMorganz/StyLua/releases/download/v0.11.0/stylua-0.11.0-linux.zip && \
        unzip stylua-0.11.0-linux.zip && \
        rm stylua-0.11.0-linux.zip && \
        chmod +x stylua

# installs selene
RUN wget -O selene-light-0.14.0-linux.zip https://github.com/Kampfkarren/selene/releases/download/0.14.0/selene-light-0.14.0-linux.zip && \
        unzip selene-light-0.14.0-linux.zip && \
        rm selene-light-0.14.0-linux.zip && \
        chmod +x selene

# installs lua (standalone binary)
RUN curl -k -o lua-5.4.2_Linux54_64_bin.tar.gz -L https://sourceforge.net/projects/luabinaries/files/5.4.2/Tools%20Executables/lua-5.4.2_Linux54_64_bin.tar.gz && \
    tar xvf lua-5.4.2_Linux54_64_bin.tar.gz && \
    mv lua54 lua && \
    rm -rf lua-5.4.2_Linux54_64_bin.tar.gz luac54

# installs luarocks (standalone binary)
RUN wget -O luarocks-3.7.0-linux-x86_64.zip https://luarocks.github.io/luarocks/releases/luarocks-3.7.0-linux-x86_64.zip && \
    unzip luarocks-3.7.0-linux-x86_64.zip && \
    mv luarocks-3.7.0-linux-x86_64/luarocks . && \
    rm -rf luarocks-3.7.0-linux-x86_64*

FROM busybox

COPY --from=builder /usr/local/bin /usr/local/bin

WORKDIR /ataraxis

RUN luarocks install luacheck

CMD stylua lua/ataraxis && \
    lua-format -i lua/ataraxis/*.lua && \
    luacheck --config .luacheckrc lua/ataraxis/*.lua && \
    selene lua/ataraxis

我尝试同时使用 Alpine 和 Busybox 作为我的 Dockerfile 的基础镜像,但是 运行 安装这些可执行文件没有成功,即使它们位于 $PATH 中包含的目录中默认情况下 (/usr/local/bin),每当我尝试 运行 其中任何一个时,都会显示以下错误:

$ lua
/bin/sh: lua: not found

我已经搜索并尝试了我能想到的每一个可能的解决方案,但仍然没有运气

TL;DR:

错误 lua: not found 是动态链接失败的症状,在尝试 运行 大陆 Linux 基于 musl-libc 的二进制文件时很常见 Linux,例如 Alpine Linux 和基于 busybox 的图像。

要解决此问题,请切换到基于 glibc 的轻量级映像(例如 Debian Slim)或在 Alpine 容器上安装 glibc。让这个为 BusyBox 工作是不切实际的。

完整解释:

一些背景知识。 libc, the standard C library, provides the C and POSIX APIs to Linux programs and is an intrinsic part of the Linux system. Most Linux distributions are based on glibc, the GNU C library. However, both Alpine Linux and BusyBox images are based on musl 标准C 库,一般与glibc 不兼容。因此,基于 glibc 的发行版(例如 Ubuntu、Debian 或 Arch Linux 构建的可执行文件将无法在 Alpine Linux 或 BusyBox 上开箱即用。

尝试 运行 glibc 可执行文件时出现链接错误。您可以通过将图像切换为 alpine 和 运行ning ldd:

来验证这一点
/ataraxis # ldd /usr/local/bin/luarocks 
    /lib64/ld-linux-x86-64.so.2 (0x7fbd14310000)
    libdl.so.2 => /lib64/ld-linux-x86-64.so.2 (0x7fbd14310000)
    libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7fbd14310000)
    libm.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fbd14310000)
    libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fbd14310000)
Error relocating /usr/local/bin/luarocks: __fprintf_chk: symbol not found
Error relocating /usr/local/bin/luarocks: makecontext: symbol not found
Error relocating /usr/local/bin/luarocks: setcontext: symbol not found
Error relocating /usr/local/bin/luarocks: __register_atfork: symbol not found
Error relocating /usr/local/bin/luarocks: __strdup: symbol not found
Error relocating /usr/local/bin/luarocks: __libc_alloca_cutoff: symbol not found
Error relocating /usr/local/bin/luarocks: __stpncpy: symbol not found
Error relocating /usr/local/bin/luarocks: __syslog_chk: symbol not found
Error relocating /usr/local/bin/luarocks: getcontext: symbol not found
Error relocating /usr/local/bin/luarocks: __open_2: symbol not found
Error relocating /usr/local/bin/luarocks: errno: symbol not found

使用 Alpine Linux 的简单而安全的解决方案是使用 Alpine 包管理器 apk 安装兼容软件。但是,Alpine 可能不存在所需的包,因为特定的包版本可能不可用。在这种情况下,您有两个选择:

  • 使用基于 glibc 的 Docker 映像,例如 Debian slim 映像(例如 debian:buster-slim - 27MB 压缩),而不是 Alpine/BusyBox
  • 在基于 musl 的映像上安装 glibc,使其与 glibc 程序兼容,但也会显着增加映像大小。

为什么不用 BusyBox:

BusyBox 不适合此自定义。由于它甚至没有包管理器,因此必须手动完成所有更改和添加。这无疑是一个极其繁琐和冗长的过程。 Alpine 仍然是一个非常轻量级的映像,您可以在其中相当简单地安装 glibc。

正在使用 glibc 将图像更新为 Alpine:

首先,将 busybox 替换为 Alpine,最好是 alpine:3.14,这是最新的 Alpine 版本(在这两个地方 - 第 1 行和第 37 行)。

其次,在COPY命令后添加如下几行:

ENV GLIBC_REPO=https://github.com/sgerrand/alpine-pkg-glibc
ENV GLIBC_VERSION=2.30-r0
RUN set -ex && \
    apk --update add libstdc++ curl ca-certificates && \
    for pkg in glibc-${GLIBC_VERSION} glibc-bin-${GLIBC_VERSION}; \
        do curl -sSL ${GLIBC_REPO}/releases/download/${GLIBC_VERSION}/${pkg}.apk -o /tmp/${pkg}.apk; done && \
    apk add --allow-untrusted /tmp/*.apk && \
    rm -v /tmp/*.apk && \
    /usr/glibc-compat/sbin/ldconfig /lib /usr/glibc-compat/lib

这将在 Alpine 容器上安装 glibc。最后,运行luarocks.
作为参考,我已经在 pastebin.

上发布了 docker 构建输出

valiano的回答很好的说明了问题。但是,他没有提到另一个解决方案,它不需要您使用 glibc:如果您从源代码构建这些程序(作为构建过程的一部分在该容器中,或者在任何其他基于 musl 的环境中)而不是仅仅下载二进制文件,它们将在您的容器中正常工作。