I see here that

Docker best practices says .. minimise the number of layers in our images

The tips given are to chain multiple lines in the dockerfile with \ so as to combine many lines of code into single commands. A specific example being:


RUN pip install jupyter

RUN pip install pandas


RUN pip install jupyter && \
pip install pandas

From this knowledge, I understand what should be done, however, I don't understand why. What are the advantages of minimising the number of layers in a Docker image? (or, conversely, what are the disadvantages of not doing so?)

  • 1
    Best is: RUN pip install jupyter pandas )) – Vasiliy Shakhunov Aug 4 at 8:41
  • Note that there is a typo in your last example - that second RUN shouldn't be there. – Andrei Mustață Aug 5 at 8:22
  • @AndreiMustață thanks for picking up on that! - fixed – stevec Aug 5 at 8:23

you should minimize the Layers of an Image for following Reasons:

  • first, you can only have (afaik) 127 Layers in an Image.
  • second: yes, you should include severals Steps in a Single run because of the Copy-On-Write functionality:

if you run:

RUN dd if=/dev/zero of=output.dat  bs=1M  count=10
RUN rm -rf output.dat

your image is still 10MB (plus some bytes for the delete Marker)

instead, you should run:

RUN dd if=/dev/zero of=output.dat  bs=1M  count=10 &&\
    rm -rf output.dat 

then, your image size is not increasing.

surely, this is not the Usual Use-Case, but, for example from a Real-World-Example:

# install terraform
ENV TF_VERSION="0.12.16"
RUN curl -LO https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip \
  && unzip terraform_${TF_VERSION}_linux_amd64.zip \
  && chmod +x ./terraform \
  && mv ./terraform /usr/local/bin/terraform \
  && rm terraform_${TF_VERSION}_linux_amd64.zip

more informations here: https://docs.docker.com/storage/storagedriver/#the-copy-on-write-cow-strategy

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.