Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use a practical example to understand the working principle of Docker volume

Shulou Source: shulou.com Published: 2022-06-02 06:48:20 09月13日 Update

To understand Docker Volume, we first need to understand how the Docker file system works. Docker images are superimposed by read-only layers of multiple file systems. When a container is started with the command docker run, Docker loads the read-only mirror layer and adds a read-write layer at the top of the mirror stack. If the running container modifies an existing file, the file will be copied from the read-only layer below the read-write layer to the read-write layer, but the read-only version of the file still exists, but it has been hidden by a copy of the file in the read-write layer.

When the Docker container is deleted and restarted through the image, previous changes in the read-write layer will be lost. In Docker, the combination of the read-only layer and the read-write layer at the top is called Union File System (federated file system), or UnionFS, which uses an important resource management technology called write-time replication. Copy-on-write (copy-on-write), also known as implicit sharing, is a resource management technology that replicates modifiable resources efficiently. For a duplicate resource, if you do not modify it, you do not need to create a new resource immediately, the resource can be shared. A new resource is created only when changes occur. This greatly reduces the consumption of unmodified resource replication. In fact, the concept of COW is no stranger to programmers, and it is widely used in various fields, such as the copy action of Internal table in ABAP, the copy implementation of Java string, and so on. Docker creates containers based on UnionFS.

Let's look at a practical example.

Use the command line docker run-- help to view the help documentation for this command. The function of-h is to specify the hostname of the container.

Create a new container using the command line:

Docker run-it-- name jerry-container-test-h CONTAINER-v / data busybox / bin/sh

The name is jerry-container-test and a volume / data is created with-v

Once created, execute cd / data in the container to enter this directory, which is still empty at this time.

Docker ps to view container status:

Now I want to know which internal directory is used on the host to implement this volume.

Use the command docker inspect jerry-container-test to view the keyword "volumes":

Get the directory where the container / data is implemented on the host:

/ var/lib/docker/volumes/96aa969033ee7e6d7ff607a0a47de5a5866613a422518ed3f86fee6240bae8cc/_data

Now I use the touch command on the host to create a file directly in this directory:

Sudo touch / var/lib/docker/volumes/96aa969033ee7e6d7ff607a0a47de5a5866613a422518ed3f86fee6240bae8cc/_data/test.s

Now switch to the container and use ls to see the files created in internal folder directly on the host using the touch command.

For more original Jerry articles, please follow the official account "Wang Zixi":

Tags: Containers files resources commands hosts directories images systems technology copy time resource management top management examples principles practical important unfamiliar people Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS OPPO Reno Shulou Information Shulou Technology