What are the mounting methods for docker?

WBOY
Release: 2022-01-28 10:23:12
Original
7247 people have browsed it

Method: 1. Use the run command, the syntax is "docker run --name test1 -it -v"; 2. Use the VOLUME instruction of dockerfile to create a mount point, the syntax is "VOLUME ["/data1" ,"/data2"]"; 3. Use container shared volumes.

What are the mounting methods for docker?

The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.

What are the mounting methods of docker?

Before introducing the VOLUME instruction, let’s take a look at the following scenario requirements:

1. The container is created based on the image. Finally The container file system includes a read-only layer and a writable layer of the image. The data persistence of process operations in the container is saved on the writable layer of the container. Once the container is deleted, the data is gone unless we manually back it up (or create a new image based on the container). Can the data persisted by the container process be saved on the host? In this way, even if the container is deleted, the data is still there.

2. When we develop a web application, the development environment is local to the host, but the running test environment is placed on the docker container.

In this case, after I modify the files (such as html, js, etc.) on the host, I need to synchronize them to the container. This is obviously more troublesome.

3. Multiple containers run a set of associated services. What if they want to share some data?

We can certainly think of various solutions to these problems. Docker itself provides a mechanism that can associate a directory on the host with a directory in the container (called a mount point, or volume). The content under the mount point on the container is the host. The contents of the directory are similar to the mount mechanism under Linux systems. In this case, when we modify the contents of the directory on the host, we do not need to synchronize the container, and it will take effect immediately for the container. Mount points can be shared by multiple containers.

Let’s introduce the specific implementation mechanism.

First pass the docker run command

1. Run the command: docker run --name test -it -v /home/xqh/myimage:/data ubuntu /bin/bash

The -v flag sets a mount point /data in the container (which is a directory in the container), and associates the contents of the /home/xqh/myimage directory on the host to /data.

In this way, operations on the /data directory in the container and operations on /home/xqh/myimage on the host are completely synchronized in real time, because these two directories actually point to the host directory. .

2. Run the command: docker run --name test1 -it -v /data ubuntu /bin/bash

The -v mark above only sets the mount point of the container, and does not Specify the associated host directory. At this time, docker will automatically bind a directory on the host. You can view it through the docker inspect command.

xqh@ubuntu:~/myimage$ docker inspect test1
[
{
    "Id": "1fd6c2c4bc545163d8c5c5b02d60052ea41900a781a82c20a8f02059cb82c30c",
.............................
    "Mounts": [
        {
            "Name": "0ab0aaf0d6ef391cb68b72bd8c43216a8f8ae9205f0ae941ef16ebe32dc9fc01",
            "Source": "/var/lib/docker/volumes/0ab0aaf0d6ef391cb68b72bd8c43216a8f8ae9205f0ae941ef16ebe32dc9fc01/_data",
            "Destination": "/data",
            "Driver": "local",
            "Mode": "",
            "RW": true
        }
    ],
Copy after login

Each piece of information under Mounts above records the information of a mount point on the container. The "Destination" value is the mount point of the container, and the "Source" value is the corresponding host directory. It can be seen that the host directory corresponding to this method is automatically created. Its purpose is not to modify it on the host, but to share it with multiple containers.

2 Create a mount point through dockerfile

The mount point created by the -v flag of the docker run command introduced above can only be valid for the created container. Mount points can be created in the image through the VOLUME directive of the dockerfile, so that all containers created through the image will have a mount point. Another difference is that the mount point created through the VOLUME command cannot specify the corresponding directory on the host and is automatically generated.

#test
FROM ubuntu
MAINTAINER hello1
VOLUME ["/data1","/data2"]
Copy after login

The above dockfile file specifies two mount points /data1 and /data2 through the VOLUME instruction.

We use docker inspect to view the container generated by the image created by this dockerfile , you can see the following information

"Mounts": [
    {
        "Name": "d411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21",
        "Source": "/var/lib/docker/volumes/d411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21/_data",
        "Destination": "/data1",
        "Driver": "local",
        "Mode": "",
        "RW": true
    },
    {
        "Name": "6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36",
        "Source": "/var/lib/docker/volumes/6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36/_data",
        "Destination": "/data2",
        "Driver": "local",
        "Mode": "",
        "RW": true
    }
],
Copy after login

You can see the information of the two mount points.

Three-container shared volume (mount point)

Next we create another container that can share the /data1 and /data2 volumes with test1 (already created container). This is done in docker run Use the --volumes-from tag, such as:

can be from different mirrors, such as:

docker run --name test2 -it --volumes-from test1 ubuntu /bin/bash
Copy after login

, or it can be the same mirror, such as:

docker run --name test3 -it --volumes-from test1 myimage /bin/bash
Copy after login

The three above Each container test1, test2, and test3 has two directories: /data1 and /data2, and the contents in the directory are shared. If any container modifies the content, other containers can obtain it.

Four Best Practices: Data Container

If multiple containers need to share data (such as persistent databases, configuration files or data files, etc.), you can consider creating a specific data container. Containers have 1 or more volumes.

Other containers share the volumes of this data container through –volumes-from.

Because the volume of the container essentially corresponds to the directory on the host, this data container does not need to be started.

For example: docker run --name dbdata myimage echo “data container”

Note: There is a volume, data sharing between containers is more convenient, but there are also many problems that need to be solved, such as permissions Control, data backup, volume deletion, etc. These contents will be introduced in subsequent articles.

Recommended learning: "docker video tutorial"

The above is the detailed content of What are the mounting methods for docker?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template