DEV Community

Discussion on: Is good idea to have bigger docker images with more layers?

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

The biggest issue (no pun intended) with having more layers is image size. Because of how Docker images work, each layer that changes file contents or adds files adds the total size of all files it adds or modifies to the final image size, even if later layers remove those changes or files.

This doesn't sound very significant, but it can have a huge impact. This is a large part of why adding packages to a base image is almost always done as a single RUN line that updates package caches, installs the required packages, and then nukes the caches, as doing each part as it's own RUN line can inflate the resultant image size by hundreds of megabytes.

There are a couple of other far less significant downsides to having lots of layers:

  • There's a small bit of overhead in terms of image size per layer even ignoring the point made above. It's usually not enough to matter, but it is there.
  • Pulls take a bit longer for each layer in the image, even if the total image size is the same, because each layer can only be extracted one at a time.
  • There's also a small amount of per-layer overhead on filesystem lookups inside running containers (stuff like listing directories or checking permissions on a path). This is also usually not enough to matter, but is still there.

On the other side of the coin, adding more layers can help you improve build times by allowing you to leverage caching better. However, this can lead to all kinds of issues if you're not careful about dropping the cache when you need to.