Open In App

What Is Docker Layered File System ?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The layered file system of Docker is a focal point in the containerization of modern-day software engineering; it has turned the software development and deployment concept upside down. Through a layered approach to filesystem organization, Docker improves the management and storage efficiency, as well as the speed of image deployment. Each layer holds filesystem modifications, so an image is built quickly and a filesystem is kept invariant. This article seeks to examine the Docker layered file systems, which are investigated based on their importance, workability and demonstrated functionality. It is important first for developers and DevOps professionals to understand Docker’s layered file system. This is because containerization technology is becoming more popular by the day.

Primary Terminologies

Docker Image

A Docker image is a lightweight, stand-alone, and executable package that has all the essential ingredients for running a software application, like the code and the runtime, dependencies, and libraries, in one single package. The latter one is a template for creating Docker containers—container instances of Docker images—that act as isolated processes on the host system. Images built with Docker use a layered file system that provides storage efficiency and the sharing of common elements across multiple images. They are immutable, which means they cannot be changed after creation, so consistency is maintained and the process is reproduced in different environments.

Layered File System

This layered file system is the central component of the Docker file architecture, and it is also known as the Union file system. It makes it possible to build Docker containers’ images by using a chain of layers that stack one upon another. Each layer is the filesystem changes presentation set, which includes file additions, modifications, or deletions. Whenever a new subdirectory is added during image creation, it only contains changes that occurred within the previous layer, which makes it possible for Docker to intelligently optimize storage space and minimize the duplication of data. With this in mind, Docker uses the copy-on-write mechanism to create layer isolation so that the changes made to the files within the containers do not affect files at the image level.

Dockerfile

A Dockerfile is a text-based configuration tool used for defining the steps to build a Docker image. It includes a set of directions, one indicating an action to be performed when the image is being built on the host machine. The instructions tell you what to do; for example, you can choose a base image, install the necessary packages, copy files, configure the environment, and execute commands when you use the container. The instructions in the Dockerfile are step-by-step adopted by Docker, which creates a number of layers. The final Docker image encompasses the application and all its dependencies.

Understanding Docker Layered File System

By means of which Docker has adopted a layered file system so as to improve the image management and resource utilization. Thus, each layer in a Docker image is the modification of a set of a filesystem, i.e. adding, changing, or removing files and directories. Docker invokes these layers when a new container is instantiated from an image and it merges them into reads & write filesystem providing a container with the isolated environment.

Step-by-Step Process

  • Image Creation: Docker images are created on the basis of a Dockerfile where the base image and other layers to be included can be specified. Each command in the Dockerfile will create the new layer.
  • Layered Architecture: Docker images constitute multiple layers, they are stacked together. The basic layer is the core filesystem, while the subsequent one is the modification of the system.
  • Efficient Storage: Docker’s layered file system uses the COW approach which ensures that only copies of the modified files should be stored as new layers. The optimization reduces the disk space and speeds up the image building and deployment.

Example:

To describe this principle, let me use an example of the process whereby we make a Docker image, drop changes and observe the layered files system.

1. Create a Dockerfile

Creating DockerFile

Creating DockerFile

That Dockerfile is to make Docker create an image from all the updated Ubuntu image and then install Nginx based on it.

FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx

2. Build the Docker Image

First open your console and then switch to the directory where the Dockerfile is located.

Then run the following command to build the Docker image:

Building Docker Image

Building Docker Image

The CMD is the 1st instruction in the docker from build command, use the instructions of the dockerfile to build a docker image named mynginx.

docker build -t mynginx .

3. View Image Layers

After building the image, we can view its layers using the docker history command:

Viewing Image Layers

Viewing Image Layers

docker history mynginx

Hence, this command shows the history of the image layers, every hanging the statements executed at every layer when an image was being built. Here’s a simplified example of what the output might look like:Here’s a simplified example of what the output might look like:

History of image layers in Dockerfile

History of image layers in Dockerfile

Every line of it makes up the one layer in the Docker image. We´re building an image while operating out each command in the Dockerfile which leads to the creation of a new layer upon the previous one. In this two-level approach Docker uses the approach where it reuses all layers when building new images and that is what greatly reduces image size tangible factors that play their role in a speeding up process.

4. Inspect Image Layers

One way to check the components for the image is by using the command docker inspect which can help to disclose the image layers.

For example:

Inspecting Image Layer

Inspecting Image Layer

These ones give a detailed description of the Docker image that may include layers, settings, Metadata, and so on.

docker inspect mynginx

Conclusion

Docker layered filesystem is the great innovation that the containerization ever produced which brought about excellent benefits in image management, storage optimization, and deployment agility. The feature of Docker that enables developers to construct Docker images as a stack of immutable layers is the very reason why they are able to develop lightweight and portable containers with no significant overhead. On the part of Docker, its layered file system adopts the copy-on-write semantics which saves resources and speeds up building and deployment workflows. Since the method of containerization for modern software development is being picked up by the latest organizations, understanding the layered file system of Docker becomes critical. This feature of Docker can help developers with the delivery pipelines in a quicker way, enhance scalability, and as a result drive innovation in the containerized computing field full of fast changes all the time.

Docker Layered File System – FQA’s

What are the advantages of Docker’s layered file system?

Docker layered file systems provide a number of features, such as efficient image management, faster image-building and deployment, and lower disk space usage because of copy-on-write semantics.

Can Docker images reuse layers?

Being based on the same parent image and having the same layers is what makes Docker images to share layers. Such sharing mechanism, called as layer caching, brings faster build times and lowers storage costs.

How are the Docker layers being created and managed?

The Docker layers are formed in the image building process, each instruction in the Dockerfile corresponds to a new layer. The layers are handled by Docker’s storage driver, which deals with creation, storage, and retrieval of layers.

What, when a file is modified in a Docker container, happens?

Once a file is changed in a Docker container, Docker achieves this via copy-on-write semantics to have a new layer with the changed file. thus, the original file is secure in the underlying image layers.

Can Docker layered file systems be modified after image creation?

No, Docker images have immutable layered file systems after creation. All changes in the filesystem imply to the creation of new layers, keeping the original unchanged.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads