Article starts from the public, the programmer blended address: mp.weixin.qq.com/s/qg8eXHobN…
1. Data volumes of containers
1. What is a data volume?
One of the concepts of Docker is to package the application and the running environment, so the life cycle of docker container is usually the same as the program running in the container, and our requirement for data is persistence, and docker containers also need a channel to share data. These requirements led to the creation of docker data volumes.
Docker data volumes are directories specially designed to bypass federated file systems (UFS) and provide access to one or more containers.
Data volumes are designed to perpetuate data and are completely independent of the container’s life cycle. Therefore, Docker does not delete the data volumes attached to it when the container is deleted, nor does it have a similar garbage collection mechanism to process the data referenced by the container
2. Features of the data volume
- Docker data volumes exist independently of docker and are separated from the lifetime of docker containers.
- It exists in a docker host.
- Docker data volume, can be directories, can also be files.
- Docker container can use data volume technology to share data with host computer.
3. Features of the data volume
- The data volume is initialized when the container is started. If the image used by the container contains data at the mount point, the data is copied to the newly initialized data volume.
- Data volumes can be shared and reused between containers.
- You can modify the contents of the data volume directly.
- Data volume changes do not affect mirror updates.
- The volume persists even if the container to which the data volume is mounted has been deleted.
4. Use the data volume
#Use the -v option to add data volumes to the container
docker run -v ~/container_data:/data -it IMAGE /bin/bash
Copy the code
In the container
root@311b7376b879:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@311b7376b879:/# cd data/
root@311b7376b879:/data# echo "hi , what's up man ?" > hi.txt
Copy the code
The host machine
huanchu-mbp:~ huanchu$ docker run -it -d -v ~/Documents/ttt:/data --name test1 nginx /bin/bash
huanchu-mbp:~ huanchu$ cat Documents/ttt/hi.txt
hi , what's up man ?
Copy the code
5. Add the access permission to the data volume
Mounted data has read and write permissions by default. However, you can also set the shared data mounted in the container to read-only, so that data modification can only be performed on the host machine, as follows:
#Ro: indicates read-only.
docker run -it -v ~/datavolume:/data:ro ubuntu /bin/bash
Copy the code
6. Use Dockerfile to create a mirror containing the data volume
Dockerfile command: VOLUME ["/data"]Copy the code
Mount points created by VOLUME command in Dockerfile cannot specify the corresponding directory on the host and are automatically generated
Dockerfile
# Version: 0.0.1
FROM ubuntu:latest
VOLUME ["/data/volume1","/data/volume2"]
RUN apt-get update RUN apt-get install -y nginx
RUN echo 'Hi, I am in your container' > /usr/share/nginx/html/index.html
EXPOSE 80
Copy the code
Build the mirror
docker build -t="mynginx2" .
Copy the code
Start the container
docker run -it --name mynginx_test mynginx2
Copy the code
Viewing data Volumes
root@e22ae166d8fa:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@e22ae166d8fa:/# cd data/
root@e22ae166d8fa:/data# ls
volume1 volume2
root@e22ae166d8fa:/data#
Copy the code
View the host mount point
Docker inspect docker inspect docker inspect
You can see information for both mount points.
"Mounts": [
{
"Type": "volume",
"Name": "596925f444bbcb7d3b75c3b844700ce8f43bd5fc8b18bb97f323e2825a1df13e",
"Source": "/var/lib/docker/volumes/596925f444bbcb7d3b75c3b844700ce8f43bd5fc8b18bb97f323e2825a1df13e/_data",
"Destination": "/data/volume1",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "0a28aed7c33a588dd74ce008d78670a5ff01dbea17e659597ebd5506bf8824cb",
"Source": "/var/lib/docker/volumes/0a28aed7c33a588dd74ce008d78670a5ff01dbea17e659597ebd5506bf8824cb/_data",
"Destination": "/data/volume2",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
Copy the code
Data volume container
1. What is a data volume container?
Named containers are used to attach data volumes to other containers for data sharing. Containers that attach data volumes are called data volume containers.
2. Create a data volume container
huanchu-mbp:Documents huanchu$ docker run -it -v ~/Documents/ttt:/data --name container_data ubuntu root@832301c940b6:/# ls bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@832301c940b6:/# cd data/ root@832301c940b6:/data# ls Dockerfile com hi.txt root@832301c940b6:/data# echo Dockerfile com/ hi.txt root@832301c940b6:/data# echo Dockerfile com/ hi.txt root@832301c940b6:/data# echo "ha ha ha !" > ha.txt root@832301c940b6:/data# ls Dockerfile com ha.txt hi.txtCopy the code
3. Method of mounting a data volume container
docker run --volumes-from [container name]
Copy the code
Create a new container and mount container_data, the data volume container you just created
huanchu-mbp:Documents huanchu$ docker run -it --name ubuntu_test --volumes-from container_data ubuntu /bin/bash
root@1d6b918c00e5:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@1d6b918c00e5:/# cd data/
root@1d6b918c00e5:/data# ls
Dockerfile com ha.txt hi.txt
root@1d6b918c00e5:/data# cat ha.txt
ha ha ha !
root@1d6b918c00e5:/data#
Copy the code
Use the inspect command to view the mount details
"Mounts": [
{
"Type": "bind",
"Source": "/Users/huanchu/Documents/ttt",
"Destination": "/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
Copy the code
Data volume backup and restoration
Data volume data backup
methods
docker run --volumes-from [container name] -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar
Copy the code
For example,
docker run --volumes-from container_data -v ~/Documents/ttt:/backup --name ubuntu_test1 ubuntu tar cvf /backup/ubuntu_test1.tar /data
Copy the code
You can view the backup data on the host
huanchu-mbp:ttt huanchu$ ls -l
total 1272
-rw-r--r-- 1 huanchu staff 191 12 24 15:48 Dockerfile
drwxr-xr-x 3 huanchu staff 96 12 12 14:45 com
-rw-r--r-- 1 huanchu staff 11 12 24 17:04 ha.txt
-rw-r--r-- 1 huanchu staff 21 12 24 14:26 hi.txt
-rw-r--r-- 1 huanchu staff 593920 12 24 18:09 ubuntu_test1.tar
Copy the code
Data volume data restoration
methods
docker run --volumes-from [container name] -v $(pwd):/backup ubuntu tar xvf /backup/backup.tar
Copy the code
For example,
The first step #
docker run -v ~/Documents/ttt:/data --name ubuntu_test2 ubuntu tar xvf /data/ubuntu_test1.tar
Step # 2
docker run -it --volumes-from ubuntu_test2 --name ubuntu_test3 ubuntu /bin/bash
Copy the code
Please scan the code or search the wechat public number “Programmer Guoguo” to follow me, pay attention to surprise ~