'cannot normalize nothing' Docker 使用共享 ARG 的集线器多阶段构建错误

'cannot normalize nothing' error on Docker Hub multi-stage build with shared ARG

我有一个多阶段构建,在阶段之间共享单个 ARG(干掉路径)。像这样:

ARG BUILD_DIR=/build

FROM alpine:latest as build
RUN apk add --no-cache gcc musl-dev
WORKDIR $BUILD_DIR
RUN echo -e '\n\
#include <stdio.h>\n\
int main (void)\n\
{ puts ("Hello, World!");}'\
>> hello.c
RUN cc -o hello hello.c

FROM alpine:latest
COPY --from=build $BUILD_DIR/hello /app
CMD ["/app"]

它在本地构建没有问题,但是自动 Docker Hub 构建失败并出现以下错误:

Step 4/9 : WORKDIR $BUILD_DIR cannot normalize nothing

我很好奇,是什么导致了这个问题(Docker Hub 上的构建引擎太旧?)。有没有比 Docker 文件中的硬编码路径更好的解决方法?

我需要在每个步骤上更新 ARG 我想重用它。引用文档:

An ARG instruction goes out of scope at the end of the build stage where it was defined. To use an arg in multiple stages, each stage must include the ARG instruction.

固定的Dockerfile是:

ARG BUILD_DIR=/build

FROM alpine:latest as build
ARG BUILD_DIR # <----------------------------------
RUN apk add --no-cache gcc musl-dev
WORKDIR $BUILD_DIR
RUN echo -e '\n\
#include <stdio.h>\n\
int main (void)\n\
{ puts ("Hello, World!");}'\
>> hello.c
RUN cc -o hello hello.c

FROM alpine:latest
ARG BUILD_DIR # <----------------------------------
COPY --from=build $BUILD_DIR/hello /app
CMD ["/app"]

这意味着我最初问题中的代码中的 ARG 值甚至不起作用。有趣的是,我的 Docker For Mac 忽略了这个问题并默默地使用空 BUILD_DIR 参数 WORKDIR 生成成功的构建。虽然 Docker Hub 自动构建器失败并明确显示错误。