The layered filesystem – Creating and Managing Container Images

Layers in Docker are intermediate Docker images. The idea is that every Dockerfile statement we execute on top of a layer changes something within the layer and builds a new one. The subsequent statement modifies the current one to generate the next one. The final layer executes the Docker CMD or ENTRYPOINT command, and the resulting image comprises several layers arranged one on top of the other. Let’s understand this by looking at a simple example.

If we pull the Flask application we built in the previous chapter, we will see the following:

$ docker pull bharamicrosystems/python-flask-redis

Using default tag: latest

latest: Pulling from bharamicrosystems/python-flask-redis

188c0c94c7c5: Pull complete

a2f4f20ac898: Pull complete

f8a5b284ee96: Pull complete

28e9c106bfa8: Pull complete

8fe1e74827bf: Pull complete

95618753462e: Pull complete

03392bfaa2ba: Pull complete

4de3b61e85ea: Pull complete

266ad40b3bdb: Pull complete

Digest: sha256:bb40a44422b8a7fea483a775fe985d4e05f7e5c59b0806a2 4f6cca50edadb824

Status: Downloaded newer image for bharamicrosystems/python-flask-redis:latest docker.io/bharamicrosystems/python-flask-redis:latest

As you can see, many Pull complete statements are beside random IDs. These are called layers. The current layer contains just the differences between the previous and current filesystem. A container image comprises several layers.

Containers contain an additional writable filesystem on top of the image layers. This is the layer where your containers modify the filesystem to provide the expected functionality.

There are several advantages of using layers instead of merely copying the entire filesystem of the container. Since image layers are read-only, multiple containers created from an image share the same layered filesystem, decreasing the overall disk and network footprint. Layers also allow you to share filesystems between images. For example, if two images come from a single base image, both images share the same base layer.

The following diagram shows a Python application that runs on an Ubuntu OS. At a high level, you will see a base layer (Ubuntu OS) and Python installed on top of it. On top of Python, we’ve installed the Python app. All these components form the image. When we create a container out of the image and run it, we get the writable filesystem on top as the final layer:

Figure 4.2 – Container layers

So, you can create multiple Python app images from the same base image and customize them according to your needs.

The writable filesystem is unique for every container you spin from container images, even if you create containers from the same image.

Leave a Reply

Your email address will not be published. Required fields are marked *