<p>you should minimize the Layers of an Image for following Reasons:</p>
<ul>
<li>first, you can only have (afaik) 127 Layers in an Image.</li>
<li>second: yes, you should include severals Steps in a Single run because of the Copy-On-Write functionality:</li>
</ul>
<p>if you run:</p>
<pre><code>RUN dd if=/dev/zero of=output.dat  bs=1M  count=10
RUN rm -rf output.dat
</code></pre>
<p>your image is still 10MB (plus some bytes for the delete Marker)</p>
<p>instead, you should run:</p>
<pre><code>RUN dd if=/dev/zero of=output.dat  bs=1M  count=10 &amp;&amp;\
    rm -rf output.dat 
</code></pre>
<p>then, your image size is not increasing.</p>
<p>surely, this is not the Usual Use-Case, but, for example from a Real-World-Example:</p>
<pre><code># install terraform
ENV TF_VERSION=&quot;0.12.16&quot;
RUN curl -LO https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip \
  &amp;&amp; unzip terraform_${TF_VERSION}_linux_amd64.zip \
  &amp;&amp; chmod +x ./terraform \
  &amp;&amp; mv ./terraform /usr/local/bin/terraform \
  &amp;&amp; rm terraform_${TF_VERSION}_linux_amd64.zip
</code></pre>
<p>more informations here: <a href="https://docs.docker.com/storage/storagedriver/#the-copy-on-write-cow-strategy" rel="nofollow noreferrer">https://docs.docker.com/storage/storagedriver/#the-copy-on-write-cow-strategy</a></p>

CC BY-SA 4.0
2020-08-05T10:07:48.937Z
2020-08-05T10:07:48.937Z
Alex
<p>I see <a href="https://www.youtube.com/watch?v=oO8n3y23b6M#t=9m10s" rel="nofollow noreferrer">here</a> that</p>
<blockquote>
<p>Docker best practices says .. minimise the number of layers in our images</p>
</blockquote>
<p>The tips given are to chain multiple lines in the dockerfile with <code>\</code> so as to combine many lines of code into single commands. A specific example being:</p>
<p>Acceptable:</p>
<pre><code>RUN pip install jupyter

RUN pip install pandas
</code></pre>
<p>Better:</p>
<pre><code>RUN pip install jupyter &amp;&amp; \
pip install pandas
</code></pre>
<p>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 <em>dis</em>advantages of <em>not</em> doing so?)</p>

CC BY-SA 4.0
2020-08-04T08:41:52.383Z
Best is:
 RUN pip install jupyter pandas ))
Vasiliy  Shakhunov
CC BY-SA 4.0
2020-08-05T08:22:37.793Z
Note that there is a typo in your last example - that second `RUN` shouldn't be there.
Andrei Mustață
CC BY-SA 4.0
2020-08-05T08:23:51.437Z
@AndreiMustață thanks for picking up on that! - fixed
stevec
<docker><containers><images>
Why is Docker best practice to minimise the number of layers in an image?
stevec
# Why is Docker best practice to minimise the number of layers in an image?

(Asked by: username_0 on 2020-08-04)

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

**Tags:** docker, containers, images

> **Comments:**
> **username_1**: Best is:  RUN pip install jupyter pandas ))  
> **username_2**: Note that there is a typo in your last example - that second `RUN` shouldn't be there.  
> **username_0**: @AndreiMustață thanks for picking up on that! - fixed  

---

## 1 answers

### Answer by username_3 on 2020-08-05. Score: 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


---

