CIsForCookies
CIsForCookies

Reputation: 12837

Can I combine COPY directives in Dockerfile to minimize number of image layers?

I saw this post, explaining how it is better to have a combined RUN directive instead of multiple RUNs, (i.e. RUN a && b && c is better than RUN a; RUN b; RUN c;).

I want to do the same for the COPY directive.

In case of the same dst, I can just do COPY /src/a/ /src/b/* /src/*/c dst/. However, when having multiple destinations, I can't do it.

I tried COPY src/a dst/a && src/b dst/b but it failed:

/var/lib/docker/tmp/docker-builder.../&&: no such file or directory

meaning the && wasn't parsed correctly.

Is there a way to have a concise COPY directive?

Upvotes: 1

Views: 257

Answers (1)

Matus Dubrava
Matus Dubrava

Reputation: 14492

No, you can't have multiple destinations per COPY directive. If you want to use just a single COPY then, if possible, build your directory structure outside the container so that it matches the expected result inside of the container and just copy the whole structure.

/var/lib/docker/tmp/docker-builder.../&&: no such file or directory

meaning the && wasn't parsed correctly.

&& was parsed correctly. It was parsed as a filename because that is how the underlying function that executes COPY command is implemented.

COPY [--chown=<user>:<group>] <src>... <dest>

Also, note that one of the reasons for having just one (or just a small bunch) of RUN directives is that each of these is executed in its own intermediate container which is immediately removed once the particular RUN directive is executed which obviously produces an additional overhead that can be avoided by simply chaining the commands.

This is not the case with COPY though. Even if you have 100s COPY directives, all of these are executed without creating and removing any intermediate containers. Personally, I am not worried about having additional COPY directive if it makes the Dockerfile more readable. This doesn't affect build speed and/or size of the container in any significant way.

Upvotes: 1

Related Questions