title: Should I minimize the number of docker layers?
body: <p>The <a href="https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/" rel="noreferrer">documentation</a> doesn't elaborate on the topic a lot. It says:</p>
  <blockquote>
  <p>Minimize the number of layers</p>
  <p>Prior to Docker 17.05, and even more, prior to
  Docker 1.10, it was important to minimize the number of layers in your
  image. The following improvements have mitigated this need:</p>
  <p>In Docker 1.10 and higher, only RUN, COPY, and ADD instructions create
  layers. Other instructions create temporary intermediate images, and
  no longer directly increase the size of the build.</p>
  <p>Docker 17.05 and higher add support for multi-stage builds, which
  allow you to copy only the artifacts you need into the final image.
  This allows you to include tools and debug information in your
  intermediate build stages without increasing the size of the final
  image.</p>
  </blockquote>
  <p>It looks like the latest Docker versions don't solve the problem of handling many layers. They rather strive to reduce their number in the final image. Most importantly, the docs don't tell <strong>why</strong> many layers are bad.</p>
  <p>I'm aware of the <a href="https://stackoverflow.com/a/39383801/2116625">AUFS limit</a> of 42 layers. It makes sense to keep the number of layers small for widely used images because it helps other images built on top of them fit the restriction. However, there are another storage drivers and images for other purposes.</p>
  <p>It is also good to keep images small for an obvious reason - they take up disk space and network bandwidth. However, I don't think that <a href="https://stackoverflow.com/questions/39223249/multiple-run-vs-single-chained-run-in-dockerfile-what-is-better">chaining RUN statements</a> and thus squashing many layers into one helps in general. In case different RUNs update different parts of the filesystem one layer and many layers together should be approximately the same in size.</p>
  <p>On the other hand, many layers allow to make use of cache and rebuild images faster. They are also pulled in parallel.</p>
  <p>I work in a small team with a private Docker registry. We won't ever meet the 42 layers restriction and care mostly about performance and development speed.</p>
  <p>If so, should I minimize the number of docker layers?</p>