skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. I don’t think that this is possible. Distinct COPY commands should be separate to maintain readability and structure.

    You can, however, have multiple sources in a single command:

    COPY --from=X src1 src2 /dest
    

    Otherwise you just need to have a series of COPY commands.

    Since the context for this is evidently a multi-stage build, you could minimise the required number of COPY commands by insuring that the files in the X layer are arranged in such a way that they can be copied across with as few COPY commands as possible. So, for example, set up the files on the X stage so that they are located in /app/dir1, /app/dir2, and /app/dir3, then simply copy /app across to next stage.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search