What is?

Here’s a look at the Docker concept:

  • Package the application and runtime environment into a container run, run can accompany the container, but we want the data requirements to be persistent
  • Containers want the possibility to share data

If the data generated by the Docker container is not generated through the Docker commit, so that the data is saved as part of the image, then when the container is deleted, the data will naturally disappear.

In order to save data in Docker we use volumes.

  • In a word: similar to our Redis RDB and AOF files

Can do

  • A volume is a directory or File that exists in one or more containers, is mounted to the container by a Docker, but is not part of a federated File System, and thus can bypass the Union File System to provide some features for storing or sharing data continuously:
  • 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

Features: 1: a data volume can share or reuse data between containers 2: changes in the volume take effect immediately 3: Changes in the data volume are not included in the update of the mirror 4: The life cycle of the data volume lasts until no container uses it

Persistence of containers

Inheritance + sharing data between containers

Data volume

In-container addition

Direct command Add

  • Command:
Docker run -v/host directory :/ container directory centos /bin/bashCopy the code
  • -v indicates volume

Docker run -v/host directory :/ The image name of the directory inside the containerCopy the code
  • Example: Create a folder between the host and the centos image
docker run -it -v /myDataVolum:/dataVolumContainer centos /bin/bash
Copy the code



A82ff6c30fbb = a82ff6C30fbb = a82ff6c30fbbdataVolumContainerThis new folder



When you enter the host computer, you will also create a myDataVolum. The data in these two folders are interlinked

  • Check whether the data volume is successfully mounted
Docker inspect Container IDCopy the code

docker inspect a82ff6c30fbb
Copy the code



  • Data is shared between containers and hosts



    Example: Test whether the host and container share data

    Create a new A.txt file on the host



    Write data to a container, such as Hello Docker!



    View what was written on the host:

  • Check whether the modified data on the host is synchronized after the container stops exiting



    Example: Exit the container first, create a new file on the host and write data. Then restart the container to see if the data being written is in sync.

    Create a new b.txt file and write: test

Docker ps-l-l: Displays the container created recently.Copy the code



Starting centos Mirroring

docker start a82ff6c30fbb
docker attach a82ff6c30fbb
Copy the code



Consistent data:

  • Commands (with permissions)
Docker run it -v/Host absolute path directory :/ Container directory :ro image nameCopy the code

Ro: read-only and cannot be written



RW: read and write



Conclusion: When setting the above permission (set ro), in the shared folder, the container can only view but not write

DockerFile add

  • Go to DockerHub to download the image: hub.docker.com/
  • Docker Hub is too slow to open, you can go to Github and search the image: github.com/docker-libr…
  • Tomcat image docker file:Github.com/dockerlibra…

  • Create the myDocker folder in the root directory and search for Tomcat. There is OpenJDK in the Tomcat Docker file, so that is why the Tomcat image package is so big.
  • One feature of docker’s downloaded images is that they are miniaturized Linux
  • The “\” in docker file is a hyphen, so a line break is needed
  • A docker file is a description file of an image template

case

mkdir mydocker
cd mydocker
Copy the code
  • You can use the VOLUME directive in Dockerfile to add one or more data volumes to the image
VOLUME["/dataVolumeContainer"."/dataVolumeContainer2"."/dataVolumeContainer3"]
Copy the code

Description:

For portability and sharing purposes, the -v host directory: container directory method cannot be implemented directly in Dockerfile. Since the host directory is host-dependent, there is no guarantee that such a specific directory will exist on all hosts.

  • The File build

  • Create two new container volumes in the root directory of the centos image and print a paragraph
# volume test
FROM centos
VOLUME ["/dataVolumeContainer1"."/dataVolumeContainer2"]
CMD echo "finished,--------success1"
CMD /bin/bash
Copy the code
  • -f is the path to the docker file, -t is the namespace, and the last dot is “. Is in the current directory. In the current directory, you can omit the -f parameter
  • The image is a layer of layers, similar to the volume, the first layer of the docker file above is centos, the second layer is container volume. We only visit the outermost layer.



    Get a new image zzYY /centos
docker build -f /mydocker/Dockerfile -t xdr630/centos .
Copy the code

After running layer by layer, you can create another custom centos image based on the container’s centos image.



Now run the container directly and create two container volumes directly under XDR630 /centos

Docker Images container nameYou can query images to see if the container exists, as in:

  • Run the container
docker run -it xdr630/centos
Copy the code

  • Create a. TAB in volume 1 of custom centos and write hello Docker!

  • There are two container volumes in the container and no corresponding host path is specified. But docker will have a default corresponding path



    Note that the centos image of the latest version maps to the virtualized host path. The virtualized host path is displayed in the virtualized value

  • Copy the above path, view on the host, and realize the host to the container can be shared data

  • After the preceding steps, the host directory address corresponding to the volume directory address in the container is known.



  • The default IP address of the host



    The default host path of the latest version is:

    /var/lib/docker/volumes/75326a87e1affc3eb80ed150430ff4aaa11c1d55ddc333f3fe0754098b964486/_data

note

Cannot open directory.: Permission denied Solution: Add a -privileged =true parameter after the host directory is mounted

Data volume container

What is?

Named containers attach data volumes, and other containers share data by attaching this (parent container). Containers that attach data volumes are called data volume containers

General introduction

  • The image zzYY /centos created in the previous step uses the template and runs containers DC01, DC02, and dc03
  • They already have container volumes /dataVolumeContainer1 /dataVolumeContainer2

Sharing volumes between containers

  • Start a parent container, DC01
  • Here –name is a self-defined name, default if not written docker randomly assigned name



  • Add content to dataVolumeContainer2

  • Dc02 /dc03 runs the dc01 -volumes-from command
docker run -it --name dc02 --volumes-from dc01 xdr630/centos
Copy the code



The newly added file was found



Dc02 /dc03 are added to dataVolumeContainer2 respectively

  • Back to DC01, you can see that the additions of 02/03 can be shared

  • Delete DC01, dc03 can be accessed after DC02 is modified

  • Dc03 can be accessed after DC02 is deleted



    In a further step

  • Create DC04, inherit DC03, and delete DC03

  • Conclusion: With the passing of configuration information between containers, the life cycle of a data volume continues until no container uses it