HiveBrain v1.2.0
Get Started
← Back to all entries
principledockerMinor

Why is Docker best practice to minimise the number of layers in an image?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
whythenumberimagedockerpracticelayersminimisebest

Problem

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

Solution

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

Code Snippets

RUN dd if=/dev/zero of=output.dat  bs=1M  count=10
RUN rm -rf output.dat
RUN dd if=/dev/zero of=output.dat  bs=1M  count=10 &&\
    rm -rf output.dat
# 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

Context

StackExchange DevOps Q#12136, answer score: 4

Revisions (0)

No revisions yet.