How to improve docker image size with layers

Click for: original source

Today we’re going to dive deeper into Docker’s union file system (UFS) layers. Author will try to explain how layers work and then how to use them properly to reduce the size of your images. By Mac Rusek.

Docker containers are made of layers. Whenever we add, change, or delete files, we add a new layer on top of the old one. A combination of all those layers or the union is what the user or container sees; it’s a top-to-bottom view. On the other hand, the actual layered perspective is different.

The article puts forward few rules, which should help you to keep overall images small:

  • Whenever we commit something, we add a new layer
  • The image is a stack of layers and the user sees only the top one
  • Image size is a sum of all layers
  • We can flatten the image by export and import, but we lose the commit history
  • We can assign several tags to one image
  • We should tag generously
  • Don’t forget to keep the latest tag for the actual latest release

From the user perspective, we can see only the files that are left after all operations, but under the hood, everything is still there. Everything we add and then remove is still in our image. It’s helpful in general, as we have access to all commits, so we know what’s happened and when it happened. Good one!

[Read More]

Tags containers software-architecture docker