Multiple RUN vs. single chained RUN in Dockerfile, which is better?
<p><code>Dockerfile.1</code> executes multiple <code>RUN</code>:</p>
<pre><code>FROM busybox
RUN echo This is the A &gt; a
RUN echo This is the B &gt; b
RUN echo This is the C &gt; c
</code></pre>
<p><code>Dockerfile.2</code> joins them:</p>
<pre><code>FROM busybox
RUN echo This is the A &gt; a &amp;&amp;\
    echo This is the B &gt; b &amp;&amp;\
    echo This is the C &gt; c
</code></pre>
<p>Each <code>RUN</code> creates a layer, so I always assumed that fewer layers is better and thus <code>Dockerfile.2</code> is better.</p>
<p>This is obviously true when a <code>RUN</code> removes something added by a previous <code>RUN</code> (i.e. <code>yum install nano &amp;&amp; yum clean all</code>), but in cases where every <code>RUN</code> adds something, there are a few points we need to consider:</p>
<ol start="0">
<li><p>Layers are supposed to just add a diff above the previous one, so if the later layer does not remove something added in a previous one, there should not be much disk space saving advantage between both methods.</p>
</li>
<li><p>Layers are pulled in parallel from Docker Hub, so <code>Dockerfile.1</code>, although probably slightly bigger, would theoretically get downloaded faster.</p>
</li>
<li><p>If adding a 4th sentence (i.e. <code>echo This is the D &gt; d</code>) and locally rebuilding, <code>Dockerfile.1</code> would build faster thanks to cache, but <code>Dockerfile.2</code> would have to run all 4 commands again.</p>
</li>
</ol>
<p>So, the question: <strong>Which is a better way to do a Dockerfile?</strong></p>