What is it
Let's take a look at the concept of Docker:
* Pack the application and operation environment to form a container operation, which can be accompanied by the container, but our data requirements hope to be persistent
* Hope it is possible to share data between containers
If the data generated by the Docker container is not generated through docker commit to generate a new image, the data will be saved as part of the image.
Then when the container is deleted, the data is naturally gone.
In order to be able to save data in docker we use volumes.
In a word: it is similar to the rdb and aof files in our redis
What can you do
A volume is a directory or file that exists in one or more containers and is mounted to the container by docker, but it is not a joint file system, so it can bypass the Union File System and provide some features for continuous storage or sharing of data:
The purpose of volume design is to persist data, completely independent of the container's life cycle, so Docker will not delete the mounted data volume when the container is deleted
Features:
1: Data volume can share or reuse data between containers
2: Changes in the volume can take effect directly
3: Changes in the data volume will not be included in the mirror update
4: The life cycle of the data volume lasts until no container uses it
Container-like endurance
Inheritance + sharing data between containers
Data volume
Direct command to add
docker run -it -v /host directory:/container directory centos /bin/bash
Check whether the data volume is successfully mounted
docker inspect container ID
Data sharing between container and host
After the container stops and exits, whether the data is synchronized after the host is modified
Command-with permissions
docker run -it -v/host absolute path directory: /container directory: ro image name
DockerFile add
Create a new 33mydocker folder in the root directory and merge it
You can use the VOLUME command in DockerFile to add one or more data volumes to the image
VOLUME["/dataVolumeContainer","/dataVolumeContainer2","/dataVolumeContainer3"]
Description:
For portability and sharing considerations, the method of -v host directory: container directory cannot be implemented directly in the Dockerfile.
Since the host directory is dependent on a specific host, there is no guarantee that such a specific directory will exist on all hosts.
file build
# volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,--------success1"
CMD /bin/bash
Generate mirror after build
Get a new mirror zzyy/centos
run container
Through the above steps, the volume directory address in the container already knows the corresponding host directory address
The host corresponds to the default address
Remarks
Docker mount host directory Docker access cannot open directory.: Permission denied
Solution: add one more --privileged=true parameter after mounting the directory
Data volume container
What is it
The named container mounts the data volume, and other containers realize data sharing by mounting this (parent container). The container that mounts the data volume is called the data volume container
General introduction
The newly created image zzyy/centos in the above step is a template and run the container doc01/doc02/doc03
They already have container volumes
/dataVolumeContainer1
/dataVolumeContainer2
Transfer sharing between containers (volumes-from)
Start a parent container first, add content to dataVolumeContainer2
dc02/dc03 inherited from dc01
--volumes-from
docker run -it --name dc02 --volumes-from dc01 zzyy/centos
dc02/dc03 are added separately in dataVolumeContainer2
Back to dc01, you can see that the data added by each 02/03 can be shared
Can dc03 be accessed after deleting dc01 and dc02?
Can dc03 be accessible after deleting dc02
Go further
Create new dc04 and inherit dc03 before deleting dc03