This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging
【Docker series 】 Docker learning six, data volume container
Dockerfile
Dockerfile is the build file used to build the Docker image. We will talk about Dockerfile in detail in the next issue, and use it here first
It is a command script, through which we can generate the mirror we want, the mirror is layered, layer by layer, the script is also a command, each command is a layer
We can look at a small example
Write your own Dockerfile to build your own image, using Ubuntu as an example
Write a simple dockerFile1
# vim dockerfile1
FROM ubuntu
VOLUME ["volume1","volume2"]
CMD echo "====successfully===="
CMD /bin/bash
Copy the code
To explain:
- FROM
Source Base image is Ubuntu
- VOLUME
Mount, can be anonymous or named mount, by default will mount to docker mounted directories
- CMD
Specify that commands can be used
Build our own mirror image
# docker build -f dockerfile1 -t xiaomotong/ubuntu .Sending Build Context to Docker Daemon 1.346GB Step 1/4: FROM Ubuntu ---> 1318b700e415
Step 2/4 : VOLUME ["volume1","volume2"]
---> Running in d7b475cacb22
Removing intermediate container d7b475cacb22
---> b8ac33cfbcfd
Step 3/4 : CMD echo "====successfully===="
---> Running in 35c98a625a9e
Removing intermediate container 35c98a625a9e
---> 67b6faf43370
Step 4/4 : CMD /bin/bash
---> Running in b2e1e0ad8d9b
Removing intermediate container b2e1e0ad8d9b
---> b26faaedefac
Successfully built b26faaedefac
Successfully tagged xiaomotong/ubuntu:latest
Copy the code
From the above, we can see that docker builds the image layer by layer, and one command is executed by one command, and one command is one layer
- docker build
Build our own mirror image
- -f
The file that specifies dockerfile
- -t
The target is the name of our Docker image
This is followed by the position where the mirror was generated
Start the container by creating the image we built
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaomotong/ubuntu latest b26faaedefac 6 minutes ago 72.8MB
# docker run -it b26faaedefac
Copy the code
After executing the above command, we can see the following directory inside the container:
We can see that volume1 and Volume2 are the anonymous mounts we just made to build the container, so we can use the Docker inspect command to see exactly where the two volumes are mounted to the host and test a synchronization
# docker inspect b29995f4178d. "Mounts": [ { "Type": "volume", "Name": "a1fd1edec5784f1153a318003bba4279b86fd2dd71b401be5864ed9b868d7332", "Source": "/var/lib/docker/volumes/a1fd1edec5784f1153a318003bba4279b86fd2dd71b401be5864ed9b868d7332/_data", "Destination": "volume1", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "975ae74c8716f5e85ccf784c716291cffda2158baf6b3f9e145ffc1ea353cb7b", "Source": "/var/lib/docker/volumes/975ae74c8716f5e85ccf784c716291cffda2158baf6b3f9e145ffc1ea353cb7b/_data", "Destination": "volume2", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], ...Copy the code
Docker Inspect shows this Volume1 and volume2 concrete is mounted to the host machine directory to/var/lib/docker/volumes/a1fd1edec5784f1153a318003bba4279b86fd2dd71b401be5864ed9b868d7332 / _da Ta, and the var/lib/docker/volumes / 975 ae74c8716f5e85ccf784c716291cffda2158baf6b3f9e145ffc1ea353cb7b / _data while forming
Let’s create a file in the container mount and test whether we can synchronize data
Create a file xiaomotong. TXT in volume1 and write the string Hello world
root@b29995f4178d:/# cd volume1 root@b29995f4178d:/volume1# echo hello world >> xiaomotong.txt root@b29995f4178d:/volume1# ll total 12 drwxr-xr-x 2 root root 4096 Aug 5 15:01 ./ drwxr-xr-x 1 root root 4096 Aug 5 Hold.. / -rw-r--r-- 1 root root 12 Aug 5 15:01 xiaomotong.txtCopy the code
Check the mount directory of the host to check whether data is synchronized
root@iZuf66y3tuzn4wp3h02t7pZ:/var/lib/docker/volumes/a1fd1edec5784f1153a318003bba4279b86fd2dd71b401be5864ed9b868d7332/_d ata# ls xiaomotong.txt root@iZuf66y3tuzn4wp3h02t7pZ:/var/lib/docker/volumes/a1fd1edec5784f1153a318003bba4279b86fd2dd71b401be5864ed9b868d7332/_d ata# cat xiaomotong.txt hello worldCopy the code
Ok, nice
So have we ever thought about how to synchronize data between a container and a container while synchronizing data between a container and a host?
Data volume container
The data volume container, such as container 1, is mounted to a directory on container 2 using the -volumes-from command. Then container 2 is the parent container and data is synchronized between the two containers, which is the data volume container
Here’s a quick example:
Made of mirror, so we just create 2 container, first create docker2, create docker1, and docker1 mounted onto docker2, and docker2 mounted directory, volume1 inside to create a file, xiaomotong. TXT. Let’s verify that docker1 volume1 also has this file
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaomotong/ubuntu latest b26faaedefac 38 minutes ago 72.8MB
# docker run -it --name docker2 b26faaedefac
# docker run -it --name docker1 --volumes-from docker2 b26faaedefac
Copy the code
B26faaedefac is the ID of the image we made ourselves
Basically using the –volumes-from command, we were able to mount both containers
docker1
root@3ed3ca51118f:/volume1# ls
root@3ed3ca51118f:/volume1# touch xiaomotong.txt
Copy the code
docker2
root@e9e1a0c46331:/volume1# ls
xiaomotong.txt
Copy the code
Sure enough, the two containers synchronize data with each other
In the example above, not only do two containers mount to synchronize data, but also multiple containers mount to the same effect, for example, another container docker3 mount to docker2, also the same effect
So how do they synchronize data?
The principle of data synchronization between containers is by copying. For example, if a file 2 is created on the mount above docker2, docker1 mounts docker2 at this time, that is to say, docker2 is the parent container, then docker1 will copy file 2 to its corresponding mount
Conversely, if Docker1 creates file 1 on its own mount, file 1 will also be copied by Docker2 on its own mount
If we delete docker2 at this point, will the files mounted by Docker1 be lost?
The answer is no, because they do it by copying, not sharing a copy
So let’s go back to the last issue when we created the mysql container. If we created multiple containers, it would be easy to synchronize their data and delete one container without losing data
Such as:
#Docker run -d -p 888:3306 -v /etc/mysq/conf. d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql1 mysql:5.7 docker run -d -p 888:3306 -v /etc/mysq/conf
#docker run -d -p 8888:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql2 - volumes - from mysql1 mysql: 5.7
Copy the code
So let’s summarize
The data volume container is used to transfer configuration information between containers, and the life cycle of the data volume lasts until no containers are used
Even if we no longer use the data volume container, the data persisted to the local host will not be deleted
References:
docker docs
Welcome to like, follow and collect
Dear friends, your support and encouragement are the motivation for me to keep sharing and improve the quality
All right, that’s it for this time
Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.
I am nezha, welcome to like the collection, see you next time ~