Tuesday, May 15, 2018

Understanding Layered Architecture of Docker Container Images

Building Docker Image From Scratch” is easy way to build any kind of containerization image. Let’s get bit more deep into it and understand what it is doing at the low level. Everyone knows in Linux everything is considered as file only. The whole operating system is nothing but a collection of files and folders.

In the previous post, we have seen there are 2 steps required as per “Dockerfile” and same has been embedded in the container layer file system. As a result we have got a container image which is nothing but a collection of layered filesystem.

Container images are templates from which containers are created. These images are not just one monolithic block, but are composed of many layers. The first layer in the image is also called the base layer:

Each layer is mapping with one command and this command is nothing but a file which will be stacked in this image. The all layers of container images are immutable or read only which means once created can’t be changed but we can delete it. In case we want to use the content of one layer in another layer, in that case we have to copy it from layer and use it in new layer. Each layer only contains the delta of changes in regard to the previous set of layers. The content of each layer is mapped to a special folder on the host system, which is usually a subfolder of "/var/lib/docker/."

When the docker engine creates a container from these images, it adds writable layer on the top of immutable or read only layers like as shown in below image:

By doing this, same immutable image can be used across various applications just by adding single writable docker layer.

As I have already mentioned, the image layers are immutable and to reuse the existing files and folders docker uses the copy-on-write strategy. With this strategy, If a layer uses a file or folder that is available in one of the low-lying layers, then it just uses it. If, on the other hand, a layer wants to modify, say, a file from a low-lying layer, then it first copies this file up to the target layer and then modifies it. Below is the snapshot of copy-on-write strategy:

As per above image, second layer want to modify file 1 which is present in base layer. Second layer will copy file 1 from base layer and then modified it. So top layer will use file 1 will be copied from layer 1 and file 2 will be copied from base layer.

People who read this post also read :

No comments: