Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Container data Volume volumes in docker

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of container data volume volumes in docker, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.

The concept of data volume

We know that when we close a running container, the data in the container is gone (if you docker commit, the data will be retained in the new image). So we need to use container data volumes to persist the data.

There is also a situation where you want data to be shared between containers, and container data volumes are also required.

In a word, data volumes are used to solve data persistence and data sharing.

A volume is a directory or file, stored in one or more containers, mounted to the container by the docker host, but not part of the federated file system, so it can bypass the federated file system to provide some features for persistent storage or sharing of data.

Volumes are designed to persist data, completely independent of the container's lifetime, so docker does not delete its mounted volumes when the container is deleted.

Features:

1) data volumes can be shared or reused between containers

2) changes to the volume can take effect directly

3) changes in the data volume will not be included in the update of the mirror

4) the life cycle of a data volume lasts until no container uses it.

5) the data volume can complete the data sharing between container to host and host to container.

Add data volumes in the container

There are two ways to add data volumes in the container, one is to add data volumes directly with the command, and the other is to add data volumes with dockerfile, which are described below. Direct command to add data volume

1) Command: docker run-it-v / absolute path directory of host: / image name of directory in container

Description:-v means volume volume

The absolute directory of the host and the directory in the container do not need to be established in advance

[root@t-docker chenzx] # docker run-it-v / myDataVolume:/dataVolumeContainer centos [root@8ad4df9ec2fd /] # lsanaconda-post.log dataVolumeContainer etc lib media opt root sbin sys usrbin dev home lib64 mnt proc run srv tmp var [root@8ad4df9ec2fd /] # cd dataVolumeContainer/

2) check whether the data volume is mounted successfully

[root@t-docker myDataVolume] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8ad4df9ec2fd centos "/ bin/bash" 5 minutes ago Up 5 minutes lucid_ swartz [root @ t-docker myDataVolume] # docker inspect 8ad4df9ec2fd "Mounts": [ {"Type": "bind" "Source": "/ myDataVolume", "Destination": "/ dataVolumeContainer", "Mode": "," RW ": true, # # Note that RW can read and write" Propagation ":" rprivate "only if it is true.

3) verify data sharing between container and host

Create files on / myDataVolume and / dataVolumeContainer, respectively, and you can see each other.

4) whether the data is synchronized after the modification of the host after the container stops exiting

Answer: also synchronize

5) data volumes with permissions

Command: docker run-it-v / Host absolute path directory: / Container directory: ro image name

Description:-v means volume volume

The absolute directory of the host and the directory in the container do not need to be established in advance

Ro: it means read only

[root@t-docker chenzx] # docker run-it-v / myDataVolume:/dataVolumeContainer:ro centos [root@c13998a78deb /] # ls [root@t-docker myDataVolume] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc13998a78deb centos "/ bin/bash" 2 minutes ago Up 2 minutes Wonderful_ Khorana [root @ t-docker myDataVolume] # docker inspect c13998a78deb "Mounts": [{"Type": "bind" "Source": "/ myDataVolume", "Destination": "/ dataVolumeContainer", "Mode": "ro", "RW": false, # # cannot be written You can only read "Propagation": "rprivate"

6) other

Sometimes there may be a host directory mounted by docker and a cannot open directory:permission denied for container access.

Solution: add an extra parameter-- privileged=true-- to the mount directory.

[root@t-docker chenzx] # docker run-it-v / myDataVolume:/dataVolumeContainer-privileged=true centosdockerfile to add data volumes

1) create a new mydocker folder under the root directory and enter

[root@t-docker chenzx] # mkdir / mydocker [root@t-docker chenzx] # cd / mydocker/

2) you can use the volume instruction in dockerfile to add one or more data volumes to the image.

Syntax: VOLUME ["/ dataVolumeContainer",'/ data/VolumeContainer2','/dataVolumeContainer3']

Note: for portability and sharing considerations, using-v host directory: container directory this method can not be directly implemented in dockerfile. Because the host directory depends on a specific host, there is no guarantee that such a specific directory exists on all hosts.

3) dockerfile construction

[root@t-docker chenzx] # cd / mydocker/ [root@t-docker mydocker] # cat Dockerfile # volume test# inherits the centos image FROM centos# to create two data volumes VOLUME ["/ dataVolumeContainer1", "/ dataVolumeContainer2"] CMD echo "finished,-success1" CMD / bin/bash under the centos directory inherited above

4) generate an image after build

[root@t-docker mydocker] # cd / mydocker/ [root@t-docker mydocker] # docker build-f / mydocker/Dockerfile-t chenzx/centos. Sending build context to Docker daemon 2.048kBStep 1 to 4: FROM centos-> 5182e96772bfStep 2 to 4: VOLUME ["/ dataVolumeContainer1", "/ dataVolumeContainer2"]-- > Running in d7a71c40c684Removing intermediate container d7a71c40c684-- > 1edef45a66a2Step 3 to 4: CMD echo "finished -success1 "--> Running in 0cad5eaf3a5eRemoving intermediate container 0cad5eaf3a5e-- > 027d129bc237Step 4 seconds ago 200MB 4: CMD / bin/bash-- > Running in 0f4088e1bf0eRemoving intermediate container 0f4088e1bf0e-- > a37b96c1c7a7Successfully built a37b96c1c7a7Successfully tagged chenzx/centos:latest [root@t-docker mydocker] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEchenzx/centos latest a37b96c1c7a7 18 seconds ago 200MB

Explanation:-t means-tag list

5) run container

[root@t-docker mydocker] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEchenzx/centos latest a37b96c1c7a7 7 minutes ago 200MB [root@t-docker mydocker] # docker run-it chenzx/centos [root@3ccb67a921de /] # [root@3ccb67a921de /] # lsanaconda-post.log dataVolumeContainer1 dev home lib64 mnt proc run srv tmp varbin dataVolumeContainer2 etc lib media opt root sbin sys usr [root@3ccb67a921de /] #

After ls, you can see that there are dataVolumeContainer1 and dataVolumeContainer2 directories in the container.

6) check the volume directory address in the container corresponding to the host directory address

[root@t-docker /] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3ccb67a921de chenzx/centos "/ bin/sh-c / bin/bash" 6 minutes ago Up 6 minutes gallant_ Jang [root @ t-docker /] # docker inspect 3ccb67a921de "Mounts": [ {"Type": "volume" "Name": "0055c21730734a9a73751de1ae1c7a5865f369e180d911490569d4aa1b78479d", "Source": "/ var/lib/docker/volumes/0055c21730734a9a73751de1ae1c7a5865f369e180d911490569d4aa1b78479d/_data", "Destination": "/ dataVolumeContainer1", "Driver": "local", "Mode": "," RW ": true "Propagation": "}, {" Type ":" volume "," Name ":" 18c5bc0fba2ec1913a7fd4997fc2181d16f2640d0c271571c0b580db89797c06 "," Source ":" / var/lib/docker/volumes/18c5bc0fba2ec1913a7fd4997fc2181d16f2640d0c271571c0b580db89797c06/_data "," Destination ":" / dataVolumeContainer2 "," Driver ":" local " "Mode": "," RW ": true," Propagation ":"}

Above, we can see that it's from the host.

/ var/lib/docker/volumes/0055c21730734a9a73751de1ae1c7a5865f369e180d911490569d4aa1b78479d/_data corresponds to the / dataVolumeContainer1 in the container

Var/lib/docker/volumes/18c5bc0fba2ec1913a7fd4997fc2181d16f2640d0c271571c0b580db89797c06/_data corresponds to the / dataVolumeContainer2 in the container

Container data volume

The named container mounts the data volume, and other containers realize data sharing by mounting the parent container, and the container of the data volume is called the data volume container.

Next, the newly created image chenzx/centos in the previous step is used as a template and runs the container dc01/dc02/dc03.

We use the newly created image chenzx/centos as the template, and the resulting container is mounted with / dataVolumeContainer1 and / dataVolumeContainer2 data volumes.

1) start a parent container dc01 first, and add new content to dataVolumeContainer2

[root@t-docker chenzx] # docker images chenzx/centos REPOSITORY TAG IMAGE ID CREATED SIZEchenzx/centos latest a37b96c1c7a7 3 hours ago 200MB [root@t-docker chenzx] # docker run-it-- name dc01 chenzx/centos [root@b786166d80d7 /] # lsdataVolumeContainer1 dataVolumeContainer2 [root@b786166d80d7 /] # cd dataVolumeContainer1 [root@b786166d80d7 dataVolumeContainer1] # touch dc01_add.txt [root@t-docker chenzx] # [root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb786166d80d7 chenzx/centos "/ bin/sh-c / bin/bash" 8 minutes ago Up 8 minutes dc01

2) dc02/dc03 inherits dc01 (--volumes-from parameter)

[root@t-docker chenzx] # docker run-it-- name dc02-- volumes-from dc01 chenzx/centos [root@e71e1991a17b /] # ls anaconda-post.log dataVolumeContainer1 dev home lib64 mnt proc run srv tmp varbin dataVolumeContainer2 etc lib media opt root sbin sys usr [root@e71e1991a17b /] # cd dataVolumeContainer1 [root@e71e1991a17b dataVolumeContainer1] # lsdc01_ add.txt [root @ e71e1991a17b dataVolumeContainer1] # touch dc02_ add.txt [root @ e71e1991a17b dataVolumeContainer1] # lsdc01_add.txt dc02_ add.txt [root @ t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe71e1991a17b chenzx/centos "/ bin/sh-c / bin/bash" 3 minutes ago Up 3 minutes dc02b786166d80d7 chenzx/centos "/ bin/sh-c / bin/bash" 14 minutes ago Up 14 minutes dc01 [root@t-docker chenzx] # docker run-it-- name dc03-volumes-from dc01 chenzx/centos [root@69c4c254cb0f /] # cd dataVolumeContainer1 [root@69c4c254cb0f dataVolumeContainer1] # lsdc01_add.txt dc02_ add.txt [root @ 69c4c254cb0f dataVolumeContainer1] # touch dc03_ add.txt [root @ 69c4c254cb0f dataVolumeContainer1] # lsdc01_add.txt dc02_add.txt dc03_ add.txt [root @ t-docker chenzx] # docker PsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES69c4c254cb0f chenzx/centos "/ bin/sh-c / bin/bash" 2 minutes ago Up About a minute dc03e71e1991a17b chenzx/centos "/ bin/sh-c / bin/bash" 7 minutes ago Up 7 minutes Dc02b786166d80d7 chenzx/centos "/ bin/sh-c / bin/bash" 18 minutes ago Up 18 minutes dc01

3) back to dc01, you can see the data added by dc02/dc03.

[root@t-docker chenzx] # docker exec-it dc01 / bin/bash [root@b786166d80d7 /] # lsanaconda-post.log dataVolumeContainer1 dev home lib64 mnt proc run srv tmp varbin dataVolumeContainer2 etc lib media opt root sbin sys usr [root@b786166d80d7 /] # cd dataVolumeContainer1 [root@b786166d80d7 dataVolumeContainer1] # lsdc01_add.txt dc02_add.txt dc03_add.txt

4) whether the dc03 can be accessed after deleting the dc01,dc02

[root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES69c4c254cb0f chenzx/centos "/ bin/sh-c / bin/bash" About an hour ago Up About an hour dc03e71e1991a17b chenzx/centos "/ bin/sh-c / bin/bash" About an hour ago Up About an hour dc02b786166d80d7 chenzx/centos "/ bin/sh-c / bin/bash" About an hour ago Up About an hour dc01 [root@t-docker chenzx] # [root@t-docker chenzx] # docker rm-f dc01dc01 [root@t-docker chenzx] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES69c4c254cb0f chenzx/centos "/ bin/sh-c / bin/bash" About an hour ago Up About an hour dc03e71e1991a17b chenzx/centos "/ bin/sh-c / bin/bash" About an hour ago Up About an hour dc02 [root@t-docker chenzx] # docker attach Dc02 [root@e71e1991a17b dataVolumeContainer1] # lsdc01_add.txt dc02_add.txt dc03_ add.txt [root @ e71e1991a17b dataVolumeContainer1] # touch dc02_ update. Txt [root @ e71e1991a17b dataVolumeContainer1] # lsdc01_add.txt dc02_add.txt dc02_update.txt dc03_add.txt

5) after deleting dc02, dc03 can still access

6) create a new dc04 to inherit dc03, and then delete dc03

[root@t-docker chenzx] # docker run-it-- name dc04-- volumes-from dc03 chenzx/centos [root@7b7f4dd0c965 /] # cd dataVolumeContainer1 [root@7b7f4dd0c965 dataVolumeContainer1] # lsdc01_add.txt dc02_add.txt dc02_update.txt dc03_add.txt Thank you for reading this article carefully. I hope the article "sample Analysis of Container data Volume volumes in docker" shared by the editor will be helpful to you. At the same time, I hope you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report