Getting to know Dockerfile for the first time and realizing data synchronization between containers

Getting to know Dockerfile for the first time and realizing data synchronization between containers

1. First acquainted with Dockerfile

Dockerfile is simply a build file and command script used to build a docker image. Mirror can be generated through this script, the mirror is layer by layer, the script commands one by one, and each command is a layer.

A simple dockerfile

# 创建一个dockerfile文件,名字可以随机 建议Dockerfile
# 文件中的内容 指令(大写) 参数
FROM centos

VOLUME ["volume01","volume02"]

CMD echo "---------end---------"
CMD /bin/bash

# 这里的每一个命令就是镜像的一层

Run your own image:

docker run -it 镜像id /bin/bash

Screenshot:
Insert picture description here
Insert picture description here
You can see that there are volume01 and volume02. These are two data volumes. The host must have its corresponding mount directory.

We can view the detailed information of the container, we can find the relevant catalog

docker inspect 容器id

Screenshot:
Insert picture description here

2. Realize data synchronization between containers

It is relatively simple to realize data synchronization between containers. You only need to run one container (with a mounted folder) first, and then add another container to another container. --volumes-from 刚刚运行的容器名
Take the above image as an example:
first run docker01

docker run -it --name docker01 ybg/centos:1.0

Screenshot:
Insert picture description here
Then run docker02

docker run -it --name docker02 --volumes-from docker01 ybg/centos:1.0

Screenshot:
Insert picture description here
Then, we create a new folder test under volume01 of docker02 to
Insert picture description here
check whether the volume01 folder of docker01
Insert picture description here
is synchronized. You can see that the two folders are synchronized.

For containers that share data, delete any one, the data of other containers will not be deleted

For example:
on the basis of just now, we delete docker01 to
Insert picture description here
view the volume01 folder of docker02, the test file is still there
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43520670/article/details/113619713