Docker containers - data volumes and data volume containers

Table of contents

1. How to manage data in docker containers?

 2. Data volume

1. Data volume principle

2. Function of data volume

3. Data volume case

Example: Mount the host directory /var/www to /data1 and /data2 in the c1 and c2 containers respectively.

Create two containers, specify the mount point, and write data in the host directory

Edit

Open two additional terminals, log in to the c1 and c2 containers respectively, and view the files in the mount point directory.

 Create a file on the C2 container, which can also be seen on the host and C1

 3. Data volume container

1. The role of data volume container

2. Data volume container case

Create a container as the data volume container and create the file

 Use –volumes-from to mount the data volumes in the test2 container to the new container

4. Container interconnection (using centos image)

5. Summary


There are two main ways to manage data in Docker containers: Data Volumes and DataVolumes Containers.

1. How to manage data in docker containers?

 2. Data volume

1. Data volume principle

Mount the configuration file directory inside the container to the specified directory on the host

  • The data volume will always exist by default, even if the container is deleted
  • The host and container are two different name spaces. If you want to connect, you need to use ssh. exec and attch are also similar to ssh login.
  • In the enterprise, the ssh login method is more dangerous and has huge security holes. Try to reduce ssh as much as possible and eliminate the frequent use of exec.
  • Therefore, those who use data volumes usually mount the files inside the container to the directory specified by the host for modification. When the files in the container frequently need to be changed, there is no need to log in. You can directly use the data volume method to modify them in the directory specified by the host. That’s it. Convenient and safe

2. Function of data volume

A data volume is a special directory used by a container and located within the container. The host's directory can be mounted to the data volume. Modifications to the data volume are immediately visible, and updated data will not affect the mirror, thus enabling data migration between the host and the container. The use of data volumes is similar to the mount operation of directories under Linux.

3. Data volume case

Example: Mount the host directory /var/www to /data1 and /data2 in the c1 and c2 containers respectively.


Create two containers, specify the mount point, and write data in the host directory

docker run -itd -v /var/www:/data1 --name c1 centos:7 bash
docker run -itd -v /var/www:/data2 --name c2 centos:7 bash

#在宿主机目录写入数据
echo "123456" >1.txt

Open two additional terminals, log in to the c1 and c2 containers respectively, and view the files in the mount point directory.

 Create a file on the C2 container, which can also be seen on the host and C1

 

 3. Data volume container

1. The role of data volume container

Let two containers share data

If you need to share some data between containers, the easiest way is to use a data volume container. The data volume container is an ordinary container that specifically provides data volumes for other containers to mount and use.

php ---->mysql wants to communicate through socket

2. Data volume container case

Requirement: Create a data volume container

#创建一个容器作为数据卷容器
docker run -it --name test1 -v /data1 -v /data2 centos:7 bash	#创建并进入容器
echo "this is test02 file" > /data1/test.txt		  	#容器内创建测试文件1
echo "THIS IS TEST2 FILE" > /data2/TEST.txt					#容器内创建测试文件2

#使用--volumes-from来挂载test2容器中的数据卷到新的容器
docker run -it --name test2 --volumes-from test1 centos:7 bash	#创建并进入容器
cat data1/test.txt											#查看测试数据是否同步
cat data2/TEST.txt

Create a container as the data volume container and create the file

 Use –volumes-from to mount the data volumes in the test2 container to the new container

4. Container interconnection (using centos image)

 Container interconnection is to establish a dedicated network communication tunnel between containers through the name of the container. To put it simply, a tunnel is established between the source container and the receiving container, and the receiving container can see the information specified by the source container.

5. Summary

This article introduces the sharing, backup and recovery of data in containers through data volumes and data volume containers. Through these mechanisms, even if the container fails during operation, users do not have to worry about data loss and only need to quickly recreate the container. That’s it. When generating the container, add the -v option to specify that the directory of the current server is mapped to the container.

Order illustrate
docker run -v data volume Create data volume
docker run -v host directory: data volume Mount host directory
docker run --volumes-from data volume container Mount the data volume container (the mount point path remains unchanged)
docker run --link source container name: alias Container interconnection

Guess you like

Origin blog.csdn.net/weixin_71429844/article/details/127369472