Search code examples
dockerdockerfiledocker-multi-stage-build

Dockerfile build ARG in COPY --from=


I am trying to set up a build process for a project and am running into an issue with using arg in the COPY command.

Part of the process is the build of a library into an image, that is used by multiple other images. The problem occurs in the following lines:

ARG BUILD_CONFIG=dev
COPY --from=company/mu_library:${BUILD_CONFIG} /some/path /other/path

According to the error message, the ${BUILD_CONFIG} is never translated into dev. When adding an echo line beforehand, the echo prints dev.

ARG BUILD_CONFIG=dev
RUN echo ${BUILD_CONFIG}
COPY --from=company/mu_library:${BUILD_CONFIG} /some/path /other/path

Does anyone have an idea how to go around it without creating duplicate stages in the dockerfile that all point to separate tags?

EDIT: Minimal Dockerfile

FROM node:12.15:0 as prod
ARG BUILD_CONFIG=dev
RUN echo ${BUILD_CONFIG}

COPY --from=test/test-library:${BUILD_CONFIG} /work/dist /work/library/dist
CMD[ "bash" ]

Error: invalid from flag value test/test-library:${BUILD_CONFIG}: invalid reference format


Solution

  • At last check, you can't use a build arg there, but you can use it in a top level from line using the multi-stage syntax. Then, you also need to define the build arg at the top level:

    ARG BUILD_CONFIG=dev
    FROM test/test-library:${BUILD_CONFIG} as test-library
    
    FROM node:12.15:0 as prod
    ARG BUILD_CONFIG
    RUN echo ${BUILD_CONFIG}
    
    COPY --from=test-library /work/dist /work/library/dist
    CMD[ "bash" ]