Roughly speaking, a Docker image contains some metadata & an array of layers, and a running container is built upon these layers by adding a container layer (read-and-write), the layers from the underlying image being read-only at that point.
These layers can be stored in the disk in different ways depending on the configured driver. For example, the following image taken from the official Docker documentation illustrates the way the files changed in these different layers are taken into account with the OverlayFS storage driver:
Next, the Dockerfile instructions RUN
, COPY
, and ADD
create layers, and the best practices mentioned on the Docker website specifically recommend to merge consecutive RUN
commands in a single RUN
command, to reduce the number of layers, and thereby reduce the size of the final image:
https://docs.docker.com/develop/dev-best-practices/
[…] try to reduce the number of layers in your image by minimizing the number of separate RUN
commands in your Dockerfile. You can do this by consolidating multiple commands into a single RUN
line and using your shell’s mechanisms to combine them together. […]
See also: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
Moreover, in your example:
RUN apt-get update -y -q
RUN apt-get install -y nginx
if you do docker build -t your-image-name .
on this Dockerfile
, then edit the Dockerfile
after a while, add another package beyond nginx
, then do again docker build -t your-image-name .
, due to the Docker cache mechanism, the apt-get update -y -q
won't be executed again, so the APT cache will be obsolete. So this is another upside for merging the two RUN
commands.