2

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:

Acceptable:

RUN pip install jupyter

RUN pip install pandas

Better:

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?)

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

1 Answer 1

4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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