Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Docker learning | Data volume container

I met Dockerfile

Dockerfile is used to build a docker image build file, is a command script

# Test Dockerfile for the first time
mkdir docker-test-volume
cd docker-test-volume
vim dockerfile Create an image from the script
Copy the code
Create a dockerfile. The name can be randomly suggested as dockerfile
# Contents directive in file (uppercase)
FROM centos 

VOLUME ["volume01"."volume02"] # Mount directory

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

---------------------------------------------
:wq
Copy the code
# build mirror
docker build -f dockerfile -t dmw/contos .

#. Is created in the current directory
# -t name:tag

docker images # view the newly created image
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
dmw/contos   latest    bcb7b9d02259   7 days ago    231MB
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Create a container from the newly created image
docker run -it bcb7b9d02259 /bin/bash
ls Volume01, volume02
Copy the code

Docker create multiple centos how to synchronize data?

# Test: Start three containers to start

docker images # view the newly created image
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
dmw/contos   latest    bcb7b9d02259   7 days ago    231MB
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


docker run -it --name docker01 bcb7b9d02259 /bin/bash
ctrl + p + q # Exit container

docker run -it --name docker02 --volumes-from docker01 bcb7b9d02259 /bin/bash
cd volume01
touch hello.java

docker + p + q

docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
7860ba22b527   bcb7b9d02259   "/bin/bash"              2 minutes ago   Up 2 minutes             docker02
cba0ed436116   bcb7b9d02259   "/bin/sh -c /bin/bash"   4 minutes ago   Up 4 minutes             docker01

docker attach docker01
cd volum01

[root@cba0ed436116 volume01]# ls -l
total 0
-rw-r--r-- 1 root root 0 Sep 23 15:42 hello.java

Hello. Java created by Docker02 has been synchronized with Docker01
Docker01 can now be called a data volume container

Create docker03 file share with Docker01

Docker02 and Docker03 can access Hello.java
# Result: Still accessible

This is a backup copy mechanism

Copy the code

Multiple MYSQL implements data sharing

Docker run -d -p 3310:3306 -v /etc/mysql.conf. d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7 Docker run -d -p 3311:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7At this point, the data of the two containers can be synchronized
# You can use docker to create mysql for local development. It is not recommended to use Docker to install mysql for actual project deployment
Copy the code

conclusion

The transfer of configuration information between containers, and the life cycle of the data volume container continues until the container is no longer in use

Once persisted locally, local data is not deleted