Docker Learning-04

Data volumes for Docker containers

1. The concept and function of data volume

 1. The concept of data volume:

        (1) The data volume is a directory or file in the host machine. When the container directory and the data volume directory are bound, the modification of the other party will be synchronized immediately.

        (2). A data volume can be mounted by multiple containers at the same time, and a container can also be mounted by multiple data volumes.

2. The role of the data volume:

        (1) Persistence of container data. When the docker container is deleted, the data generated in the container will also be destroyed. But the data volume in the host machine is still preserved.

        (2) The external machine can communicate indirectly with the container through the data volume.

        (3) Data exchange can be performed between containers through data volumes.

 

Two, configure the data volume

When creating a startup container, use the -v parameter to set the data volume:

sudo docker run ... -v <宿主机目录(文件)>:<容器内目录(文件)> ...

Precautions:

1. The directory must be an absolute path.

2. If the directory does not exist, it will be created automatically.

3. Multiple data volumes can be mounted.

4. Two containers can mount the same data volume to realize communication between containers.

3. Configure the data volume container

Create a container, mount a directory, let other containers inherit from this container (--volumes-from), and this container becomes a data volume container.

1. Create and start the c3 data volume container, and use the -v parameter to set the data volume

sudo docker run -it --name=c3 -v /volume centos:7 /bin/bash

2. Create and start c1 and c2 containers, and use the --volumes-from parameter to set the data volume

sudo docker run -it --name=c1 --volumes-from c3 centos:7 /bin/bash
sudo docker run -it --name=c2 --volumes-from c3 centos:7 /bin/bash

Guess you like

Origin blog.csdn.net/xiao_qs/article/details/130659603