Thank B station crazy god learning resources, interested can go to search: meet crazy god said.
Volume 1 technical introduction
Now we know that the idea behind Docker is to package the application and environment into images so that we can run the container.
But now there is a problem, for example, my container mysql stored a lot of data, so if the container is deleted, will the data be lost? This is clearly inappropriate.
A new requirement arises: store data outside of the container, such as locally. The solution is volume technology, which synchronizes data generated in containers locally and allows them to share data.
Volume technology, in plain English, is directory mounting, where directories in containers are mounted to Linux. As shown in the figure, place the/usr/mysql
Mounted to external Linux/home/mysql
. Now not only mysql container can be used, the other two containers can also be used, to achieve data sharing.
Therefore, the key words of volume technology: persistence, synchronization, data sharing.
2. Use data volumes
Run the -v command.
Docker run -v Host directory: container directoryCopy the code
Now I want to start the centos image and mount the container /home directory to the host /home/test/ :
docker run -it -v /home/test:/home centos /bin/bash
Copy the code
You can see that the container started successfully, so I have an extra window for convenience.
Use the Docker inspect command to view the container and see the mount condition.
docker inspect 370ff4e87083
Copy the code
You can find Mounts, and you can see the binding details.
The container’s /home directory is now empty, as is the corresponding host’s /home/test/ directory.
Then I create a new file under /home, and check the /home directory on the host. The file has been synchronized.
The inverse
Second. TXT file from the host directory, and then go to the container again.
Stop the container
If the container stops, I delete all the files outside, and when I start the container again and go in, it’s synchronized.
Restart the container, go to the container /home to view, also empty.
I believe that now should be able to experience the role of container volume, such as some containers involved in the need to change the configuration, then after mounting to the host directory, there is no need to enter the container to modify, directly modify outside, container automatic synchronization.