Containers are pretty amazing. They allow simple processes to act like virtual machines. Underneath this elegance is a set of patterns and practices t

Understanding Container Image Layers

submited by
Style Pass
2024-05-14 23:30:04

Containers are pretty amazing. They allow simple processes to act like virtual machines. Underneath this elegance is a set of patterns and practices that ultimately makes everything work. At the root of the design is layers. Layers are the fundamental way of storing and distributing the contents of a containerized file system. The design is both surprisingly simple and at the same time very powerful. In today’s post, I’ll explain what layers are and how they work conceptually.

When you create an image, you typically use a Dockerfile to define the contents of the container. It contains a series of commands, such as:

Under the covers, the container engine will execute these commands in order, creating a “layer” for each. But what is really happening? It’s easiest to think of each layer as a directory that holds all of the modified files.

To share these layers, the easiest approach is to create a compressed .tar.gz for each directory. To reduce the total file size, any files that are unmodified copies of data from a previous layer would be removed. To make it clear when a file was deleted, a “whiteout file” could be used as a placeholder. The file would simply prefix .wh. to the original filename. For example, the fourth layer would replace the deleted file with a placeholder named .wh.message.txt. When a layer is unpacked, any files that start with .wh. can be deleted.

Leave a Comment