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
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: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:The usage of the copy command is explained better here.
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:
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 theX
layer are arranged in such a way that they can be copied across with as fewCOPY
commands as possible. So, for example, set up the files on theX
stage so that they are located in/app/dir1
,/app/dir2
, and/app/dir3
, then simply copy/app
across to next stage.