简体   繁体   中英

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 RUN s, (ie 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM