Docker container data volume [4]


 What is it?

Let’s have a look firstDockerThe concept of the company:

  • The application and running environment will be packaged to form a container to run. Running can be accompanied by the container, but we hope that the data requirements are persistent
  • Containers want to share data

If the data generated by the docker container does not pass thedocker commitA new image is generated to save the data as a part of the image. When the container is deleted, the data will be lost naturally.

In order to save the data, we use thevolume

In a word: it’s a bit similar to RDB and AOF in redis


What can I do

A volume is a directory or file that exists in one or more containers and is controlled by thedockerIt is mounted to a container, but does not belong to a federated file system. Therefore, it can bypass the union file system and provide some features for continuous storage or sharing of data

The purpose of volume design is data persistence, which is completely independent of the life cycle of the containerDockerThe data volumes mounted on the container are not deleted when the container is deleted.


  1. Data volumes can share or reuse data between containers
  2. Changes in the volume can take effect directly
  3. Changes in the data volume are not included in the update of the mirror
  4. The life cycle of a data volume container lasts until no container uses it (–volumes from


  • Persistence of container data
  • Inheritance and data sharing between containers


Data volume

  • Direct command add

Docker run - it - V / host absolute path directory / container directory image name
Docker run - it - V / host absolute path directory / container Directory: RO image name // with command, specify access permission, RO: read only

Check whether the data volume is mounted successfully:

Docker inspect container ID


  • Using dockerfile to add

Create a new mydocker folder in the root directory and enter

Available atDockerfileUsed inVOLUMECommand to add one or more data volumes to the mirror



Dockerfile construction

For the compilation of dockerfile, you can refer to the dockerfile files of each image in dockerhub, such as Tomcat:

#volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,-------success1"
CMD /bin/bash

Image generation after build


Get a new image ZzYy / CentOS

Run container

Through the above steps, the volume directory address in the container already knows where the corresponding host directory is



Can’t open directory, permission denied appears when docker accesses the host directory

Solution: add one more after mounting the directory–privileged=trueParameters are OK


Data volume container

  • What is it?

Named container mounts data volume, other containers realize data sharing by mounting this (parent container). The container that mounts data volume is called data volume container

  • Transfer sharing between containers (- – volumes – from)

Docker run - it -- name dco2 -- volumes from DC01 zzygcenos // DC01 is the container created first, and dco2 inherits DC01 to realize data sharing

The data volume is mounted by the parent container (DC01). If DC01 is deleted after dc02 and dc03 mount DC01, the data volume will still be valid.

With the transfer of configuration information between containers, the life cycle of data volume continues until no container uses it.