Docker Overlay2 Inode

Introduction

Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications. It allows applications to run inside containers, which are lightweight, isolated environments that share the host operating system's kernel. Containers use a layered file system to provide a consistent and efficient way to distribute and run applications across different environments.

Overlay2 is the default storage driver used by Docker on Linux. It is a copy-on-write (CoW) driver that enables the creation of multiple layers or images on top of a base image. Each layer is read-only and represents a file system snapshot. When changes are made to a container, new layers are created on top of the base image, and only the modified files are stored in these layers.

Inode is a data structure in a file system that contains metadata about a file or directory, such as its size, permissions, and timestamps. Inodes are used by the operating system to access and manage files efficiently. Docker Overlay2 uses inodes to track changes made to files and directories in containers and to provide copy-on-write functionality.

Anatomy of Overlay2 Inodes

Overlay2 uses inodes to track changes made to files and directories in a container. Each file or directory in the container has its own inode, which contains metadata about the file or directory. These inodes are stored in the lower layer of the Overlay2 file system.

When a container is started, Docker mounts the Overlay2 file system, which consists of one or more layers, onto the host's file system. The lower layer is read-only and represents the base image, while the upper layer is writable and contains the changes made to the container.

Overlay2 File System

The Overlay2 file system uses inodes to keep track of the changes made to files and directories. When a file or directory is modified or created in a container, Overlay2 creates a new inode in the upper layer. This new inode contains the metadata about the modified or created file or directory.

Code Example

To illustrate how Overlay2 inodes work, let's consider a simple example. We will create a Docker container and modify a file inside it.

First, let's create a Dockerfile with the following contents:

FROM ubuntu:latest
RUN echo "Hello, World!" > /app/hello.txt

This Dockerfile creates a new image based on the latest Ubuntu image and adds a file named hello.txt to the /app directory with the content "Hello, World!".

Next, let's build the Docker image by running the following command:

$ docker build -t myimage .

Once the image is built, we can create a container and modify the hello.txt file:

$ docker run -it myimage /bin/bash
$ echo "Modified Hello, World!" > /app/hello.txt

In this example, we created a container based on the myimage image and opened a shell inside it. We then modified the hello.txt file by replacing its content with "Modified Hello, World!".

Now, let's inspect the Overlay2 file system to see how the changes are stored. We can use the ls command with the -li option to display the inodes:

$ docker exec <container_id> ls -li /app

The output will show the inode number, file permissions, and other metadata for the hello.txt file. If we run the command before and after modifying the file, we will see that the inode number changes, indicating that a new inode was created for the modified file.

Conclusion

Docker Overlay2 is a copy-on-write storage driver that uses inodes to track changes made to files and directories in containers. Inodes are data structures in a file system that contain metadata about files and directories. Overlay2 creates new inodes in the upper layer of the file system when changes are made to files or directories in a container. This allows Docker to provide efficient and reliable copy-on-write functionality.

By understanding how Overlay2 inodes work, developers can gain insights into the inner workings of Docker's storage system. This knowledge can help diagnose and troubleshoot issues related to file system performance and container storage.

References

  • Docker Documentation: [
  • OverlayFS Documentation: [