Search code examples
dockerdockerfile

How to combine multiple docker copies from reference image into single line?


How can I combine multiple COPY commands in a dockerfile that all use --from=X? I'm trying

COPY --from=x /dir1 /dir1 && \
--from=x /dir2 /dir2 && \
--from=x /dir3 /dir3 && \
--from=x /dir4 /dir4

but this gives me the error ERROR: failed to solve: circular dependency detected on stage: x


Solution

  • From my experience, Docker doesn’t support directly mapping multiple source directories to multiple destinations in one command like you did. Each COPY command can only handle a single source to destination mapping or copy multiple sources to one directory.

    You would typically see a COPY command structured like yours if you were copying multiple files into one directory:

    COPY --from=x /dir1/* /dir2/* /dir3/* /dir4/* /destination/
    

    This would copy all files from these directories into a single /destination/ directory, which is not what you want.

    For what you’re trying to achieve, you need to use multiple COPY commands, each specifying the correct source and destination paths:

    COPY --from=x /dir1/ /dir1/
    COPY --from=x /dir2/ /dir2/
    COPY --from=x /dir3/ /dir3/
    COPY --from=x /dir4/ /dir4/
    

    The usage of the copy command is explained better here.