''Geometry is cool'' can't be said to be on the hot list, I said it was the container volume that you didn't learn docker from me


One, what is it?

背景

  • Docker packages the application and running environment to form a container operation, and the operation can be accompanied by the container, but our data requirements hope to be persistent
  • It is hoped that it is possible to share data between containers

If the data generated by the Docker container does not generate a new image through docker commit, so that the data is saved as part of the image, then when the container is deleted, the data will naturally be gone. In order to save the data in the docker, we use it .

A bit similar to the rdb and aof files in our Redis; or the configuration center; or the data volume in k8s

Second, what's the use?

  • Persistence of containers
  • Inheritance + shared data between containers

A volume is a directory or file that exists in one or more containers and is mounted to the container by docker, but does not belong to the Union File System, so it can bypass the Union File System to provide some features for persistent storage or sharing of data:

The design purpose of the volume is the persistence of data, which is completely independent of the life cycle of the container, so Docker will not delete the mounted data volume when the container is deleted.

特点:

  1. Data volumes to share or reuse data between containers
  2. Changes in the volume can take effect directly
  3. Changes in data volumes will not be included in mirrored updates
  4. The lifecycle of a data volume lasts until no containers use it

3. Data volume

3.1 Direct command addition

The previous chapter "[Docker Guide・Ⓓ³]Deep Analysis of Docker Images" explained how to make your own image issa/myubuntu, so this time, we still start the data volume exercise with this.

insert image description here

3.1.1 Mount data volume

Order: docker run -it -v /宿主机绝对路径目录:/容器内目录:[权限] 镜像名

[permission]: ro/rw (read-only/read-write)
Change the read and write permissions by adding ro and rw to the path in the container
ro readonly # read only
rw readwrite # read and write
The default rw, if it is ro, this path can only be operated by the host, and cannot be operated inside the container

  1. New command:dockder run -it -v /tmp:/tmp/test issa/myubuntu:1.14 bash
  2. In the host/tmp directory, create a new command:touch hello.txt
  3. Check whether the hello.txt file is updated synchronously in the test directory in the container
  4. Create a new command in the current directory of the container: tail -f hello.txt, observe ing
  5. In the host/tmp directory, create a new command: echo i love u CSDN 步尔斯特 >> hello.txt, observe whether there is output in the container

insert image description here
insert image description here

3.1.2 Check whether the data volume is successfully mounted

docker inspect 容器ID

insert image description here

3.1.3 Data Sharing Between Containers and Hosts

3.1.4 After the container stops and exits, whether the data is synchronized after the host is modified

3.1.5 Commands (with permissions)

docker run -it -v /宿主机绝对路径目录:/容器内目录:ro 镜像名

3.2 DockerFile add

  1. Create a new mydocker folder in the root directory and enter

  2. You can use the VOLUME command in the Dockerfile to add one or more data volumes to the image.
    VOLUME["/dataVolumeContainer","/dataVolumeContainer2","/dataVolumeContainer3"]
    说明:
    For portability and sharing considerations, use -v host directory:container The directory method 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 exists on all hosts.

  3. File build

    # volume test 
    FROM issa/ubuntu:1.14 
    VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"] 
    CMD echo "finished,--------success!" 
    CMD /bin/bash 
    
  4. Generate an image after build to
    get a new image issa/ubuntu:1.14

  5. run container

  6. Through the above steps, the volume directory address in the container is already known

  7. The host corresponds to the default address

3.3 Three ways to mount

所有docker容器内的卷,没有指定目录的情况下都是在/var/lib/docker/volumes/xxxxx/_data

3.3.1 Specify the path to mount

-v /主机路径:容器内路径
This is the method used above

3.3.2 Anonymous mount

-v 容器内路径

3.3.3 Named Mount

-v 卷名:容器内路径

3.4 Remarks

Docker mounts the host directory Docker access appears cannot open directory .: Permission denied
Solution: add one more --privileged=trueparameter after the mount directory

3.5 Related Commands

  1. View all volumes
    docker volume ls
  2. View a volume
    docker volume inspect 卷名

5.4 Data Volume Container

5.4.1 Introduction

The named container mounts the data volume, and other containers realize data sharing by mounting this (parent container), and the container that mounts the data volume is called the data volume container

The image created in the above step is used as a template and runs the container dc01/dc02/dc03

They already have container volumes
/dataVolumeContainer1
/dataVolumeContainer2

5.4.2 Transfer sharing between containers (--volumes-from)

  1. First start a parent container dc01
    to add content in dataVolumeContainer2
  2. dc02/dc03 inherited from dc01
    --volumes-from
    docker run -it --name dc02 --volumes-from dc01 issa/ubuntu:1.14
    dc02/dc03 added content to dataVolumeContainer2 respectively
  3. Go back to dc01 and you can see that everything added by 02/03 can be shared.
  4. Delete dc01, whether dc03 can be accessed after dc02 is modified
  5. Can dc03 be accessed after dc02 is deleted?
  6. one step further
  7. Create a new dc04 to inherit dc03 and then delete dc03
  8. Conclusion: The transfer of configuration information between containers, the life cycle of the data volume continues until no container uses it

Guess you like

Origin blog.csdn.net/CSDN_SAVIOR/article/details/124427057