This is the 23rd day of my participation in the August More Text Challenge
Introduction to the
Volumes are designed to persist data and are completely independent of the lifetime of the container, so Docker does not delete its mounted data volumes when the container is deleted
Currently Docker provides three different ways to mount data from the host machine to the container:
1. Volume: part of the Docker management host file system, located in the /var/lib/docker-volumes directory by default
Currently, all container data is stored in this directory. Since no volume is specified at creation time, Docker creates many anonymous volumes by default (the names of the long ids above).
“Bind virtualized” means it can be stored anywhere on the host system. (More common way)
3. TMPFS: It is mounted and stored in the memory of the host system without being written to the file system of the host. (In a way you wouldn’t normally)
The basic use
Managing Data Volumes
docker volume create test Create a custom container volume
docker volume ls View all container volumes
docker volume inspect test # View details about the specified container volume
docker volume rm test # delete container data volume
Copy the code
The use of the volume
Create a new container, where -p specifies the exposed port and -v specifies the data volume
docker run -d -it --name test-nginx -p 8080:80 -v test-nginx:/usr/share/nginx/html nginx
Copy the code
The docker volume LS shows the data volume we mounted
The default data volumes are mounted to the /var/lib/docker-volumes directory
In the data volume directory, you can find that the contents of the mounted directory and the directory in the container are synchronized
And then let’s test it out
The contents of the data volume can be persisted. If you need to create an Nginx container next time, reuse the files in the current data volume.
Create a new container to test that the name of the mounted volume is the same as above
docker run -d -it --name test-nginx-2 -p 9090:80 -v test-nginx:/usr/share/nginx/html nginx
Copy the code
Accessing the exposed port, you can see the same contents as the container we created above
Bind Mounts to use
Create a new container, expose 10000 ports, and mount /test-nginx as /usr/share/nginx/html
docker run -d -it --name test-nginx-3 -p 10000:80 -v /test-nginx:/usr/share/nginx/html nginx
Copy the code
test
After entering the container, you can see that the contents of the container are the same as the contents of the host directory
Just like volumes, when you clean up the container, the files in the mount directory are still there and will not disappear with the end of the container for data persistence.
After the container is deleted, the files in the mounted directory still exist
Recommended reading
Linux Shell programming basics!
Linux Sudo and Sudoers
Samba server deployed on Linux!
Linux Zabbix 5.0 installation details!
Docker docker-compose: docker-compose
Docker Dockerfile file details!