principledockerMinor
Why is Docker best practice to minimise the number of layers in an image?
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
Acceptable:
Better:
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?)
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 pandasBetter:
RUN pip install jupyter && \
pip install pandasFrom 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:
if you run:
your image is still 10MB (plus some bytes for the delete Marker)
instead, you should run:
then, your image size is not increasing.
surely, this is not the Usual Use-Case, but, for example from a Real-World-Example:
more informations here: https://docs.docker.com/storage/storagedriver/#the-copy-on-write-cow-strategy
- 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.datyour 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.datthen, 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.zipmore 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.datRUN 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.zipContext
StackExchange DevOps Q#12136, answer score: 4
Revisions (0)
No revisions yet.