DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

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

So what is your suggestion about that? Would you rather to have a few or lots of layers in your application's docker image?

https://devops.stackexchange.com/q/10919/4396

Top comments (2)

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.

Collapse
 
jessekphillips profile image
Jesse Phillips

I'd say you want the right layers. So as it said, a php installed layer then application. The multiparty docker can be good to limit final image size, but don't use it as a poor man's build system.