Previous review:
- First Docker and install | Docker series
- Use the Docker mirror | Docker series
- Operating Docker container | Docker series
We all know that containers are created and deleted at any time, but what about the data in the container when deleted? Manual backup every time? Of course not, Docker is very considerate to provide data persistence solution, called data volume.
Using Volume has four advantages:
-
Volumes can be shared and reused between containers and between containers and hosts.
-
The volume is changed at a mount location, and all places that use the volume are updated at the same time.
-
Volume updates do not affect mirroring.
-
The volume can be deleted using the docker volume rm [Volumes name] command until no more containers use it.
As you can see, in addition to data persistence, another important point is to synchronize the host’s files to the container and be able to update them in real time. This allows you to mount the source code directory into the container, and when code needs to be modified, the native code is automatically synchronized to the container, which is very convenient when developing tests.
Creating a Data Volume
Create a data volume using the docker volume create command:
$ docker volume create --name test
Copy the code
Viewing data Volumes
Run the docker volume ls command to view the list of data volumes:
$ docker volume ls
DRIVER VOLUME NAME
local test
Copy the code
Docker Volume Inspect
$ docker volume inspect test
[
{
"CreatedAt": "2020-06-22T10:25:46Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/test/_data",
"Name": "test",
"Options": {},
"Scope": "local"
}
]
Copy the code
Run the /var/lib/docker-volumes directory to add volumes to your database.
There was a hickey in testing this place, which worked perfectly under Linux, but on my own Mac, the CD never found the mount directory, even though the details showed it.
The reason is that on the Mac, Docker starts a virtual machine to run the actual Docker process, so how to log in to the Docker VIRTUAL machine? Use the following command:
$ screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
Copy the code
If the docker version is less than 18.06, use the following command:
$ screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
Copy the code
After executing the command, a new interface will be displayed. Press Enter to enter the Docker VIRTUAL machine, where the directory will be found.
docker-desktop:~#
docker-desktop:~# cd /var/lib/docker/volumes/
docker-desktop:/var/lib/docker/volumes# ls
metadata.db test
Copy the code
Binding a Data Volume
To start a container, run the -v host: container command to bind data volumes.
$ docker run -d -v test:/root centos /bin/sh -c "while true; do echo hello world; sleep 1; done"
$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 75F5A72A2F21 centos "/bin/sh -c 'while t..." 44 hours ago Up 44 hours charming_curieCopy the code
View the container details to see the specific binding information:
$ docker inspect 75f. "Mounts": [ { "Type": "volume", "Name": "test", "Source": "/var/lib/docker/volumes/test/_data", "Destination": "/root", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ], ...Copy the code
The host directory can be volume name or specific path, for example:
$ docker run -d -v /home/test:/root centos /bin/sh -c "while true; do echo hello world; sleep 1; done"
Copy the code
In this way, all file changes in the corresponding directory are synchronized.
Deleting a Data Volume
Use the docker volume rm command to delete a data volume.
Use the docker volume prune command to clean up unwanted data volumes.
Data volume container
If you want to share data between containers, you can use a data volume container. In other words, a container is used to mount other containers.
First, create a container dbData and create a data volume to mount to dbData:
$ docker run -it -v /dbdata --name dbdata centos
Copy the code
Use the –volumes-from parameter to start another container:
$ docker run -it --volumes-from dbdata --name db1 centos
$ docker run -it --volumes-from dbdata --name db2 centos
Copy the code
If one of the three containers changes its dbData directory, all the other containers can synchronize.
Reference Documents:
www.jianshu.com/p/8c22cdfc0…
Introduction and Actual Combat of Docker Technology
Past highlights:
- First Docker and install | Docker series
- Use the Docker mirror | Docker series
- Operating Docker container | Docker series
Tech Blog:
Github.com/yongxinz/te…
Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.