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

What is a Docker container data volume

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the Docker container data volume". In the daily operation, I believe that many people have doubts about what is the Docker container data volume. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the question of "what is the Docker container data volume?" Next, please follow the editor to study!

Introduction of a container dataset volume

Let's first take a look at the philosophy of Docker:

Package the application and running environment to form a container to run, which can be accompanied by the container, but our requirements for data are expected to be persistent.

Containers want it to be possible to share data

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

Bottom line: somewhat similar to the rdb and aof files in our Redis

Second function and characteristics

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

Volumes are designed to persist data, which is completely independent of the lifetime of the container, so Docker will not delete its mounted data volumes when the container is deleted.

Features:

1: data volumes can be shared or reused 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 lasts until no container uses it

Persistence of containers

Inherit between containers + share data

Use 1 direct command to add 1.1 command docker run-it-v / host directory: / container directory centos / bin/bashdocker run-it-v / host absolute path directory: / container directory image name docker run-it-v / opt/docker/myDataValume:/data/VolumeContainer centos

The command will create a directory in the host and container, respectively.

1.2 check whether the data volume is mounted successfully # docker inspect container IDCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3f1e57169493 centos "/ bin/bash" >

1.3 data sharing between container and host 1.4 after the container stops exiting, whether the data is synchronized after the host is modified

Create a file in the host and view it after starting the container

1.5 command (with permission, read-only) docker run-it-v / host absolute path directory: / container directory: ro image name docker run-it-v / opt/docker/myDataValume:/dataVolumeContainer:ro centos

The host can edit the shared file and read-only in the container

View dockerfile

Docker inspect 621e34eea2a3

2 DockerFile add 2.1 New / opt/docker and enter 2.2 create Dockerfile file and use VOLUMN directive to add one or more data volumes to the image 2.3 File build FROM centosVOLUME ["/ dataVolumnContainer1", "/ dataVolumnContainer2"] CMD echo "finish -success "CMD / bin/bash2.4 build to generate an image root@zhanghl:/opt/docker# docker build-f / opt/docker/Dockerfile-t zhl/centos .Sending build context to Docker daemon 3.584kBStep 1 finish 4: FROM centos-> 300e315adb2fStep 2 dataVolumnContainer2 4: VOLUME [" / dataVolumnContainer1 "," / dataVolumnContainer2 "]-> Running in 6c05cec1141fRemoving intermediate container 6c05cec1141f-> b017ed96a615Step 3max 4: CMD echo" finish -success "--> Running in 8e2158b75c7dRemoving intermediate container 8e2158b75c7d-- > ea3a76a3d696Step 4 seconds ago 209MBzhl/mytomcat 4: CMD / bin/bash-- > Running in 7f2a18bbf3f0Removing intermediate container 7f2a18bbf3f0-- > e4998e64deceSuccessfully built e4998e64deceSuccessfully tagged zhl/centos:latestroot@zhanghl:/opt/docker# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEzhl/centos latest e4998e64dece 41 seconds ago 209MBzhl/mytomcat 1.1 a636ea3caaed 2 days ago 672MBmongo latest 995ccc33e58f 6 days ago 449MBtomcat latest c0e850d7b9bb 7 days ago 667MBhello-world latest d1165f221234 7 weeks ago 13.3kBcentos latest 300e315adb2f 4 months ago 209MB2.5 run Container root@zhanghl:/opt/docker# docker run-it zhl/centos [root@9a463e73a1d9 /] # pwd/ [root@9a463e73a1d9 /] # ls-ltotal 56lrwxrwxrwx 1 root root 7 Nov 315: 22 bin-> usr/bindrwxr-xr-x 2 root root 4096 Apr 30 13:14 dataVolumnContainer1drwxr-xr-x 2 root root 4096 Apr 30 13:14 dataVolumnContainer2drwxr-xr-x 5 root root 4096 Apr 30 13:14 devdrwxr-xr-x 1 root root 4096 Apr 30 13:14 etcdrwxr-xr-x 2 root root 4096 Nov 3 15:22 homelrwxrwxrwx 1 root root 7 Nov 3 15:22 lib-> usr/liblrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64-> usr/lib64drwx- 2 root root 4096 Dec 4 17 Dec 37 lost+founddrwxr-xr-x 2 root root 4096 Nov 3 15:22 mediadrwxr-xr-x 2 root root 4096 Nov 3 15:22 mntdrwxr-xr-x 2 root root 4096 Nov 3 15:22 optdr-xr-xr-x 260 root root 0 Apr 30 13:14 procdr-xr-x--- 2 root root 4096 Dec 4 17:37 rootdrwxr-xr-x 11 root root 4096 Dec 4 17:37 runlrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin-> usr/sbindrwxr-xr-x 2 root root 4096 Nov 3 1522 Srvdr-xr-xr-x 13 root root 0 Apr 30 13:14 sysdrwxrwxrwt 7 root root 4096 Dec 4 17:37 tmpdrwxr-xr-x 12 root root 4096 Dec 4 17:37 usrdrwxr-xr-x 20 root root 4096 Dec 4 17:37 var [root@9a463e73a1d9 /] # 2.6 View the directory of the corresponding host

Because no host directory is specified, docker generates a default directory

Root@zhanghl:/home/zhanghl# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9a463e73a1d9 zhl/centos "/ bin/sh-c / bin/bash" 2 minutes ago Up 2 minutes epic_paniniroot@zhanghl:/home/zhanghl# docker inspect 9a463e73a1d9

View in the host

Zhanghl@zhanghl:/var/lib/docker/volumes/1119e11a0e8e98075d0d8f2fc6a01cb27b5a57b47a7b98668b8aa9b534408ab3/_data$ lscontainer01.txt

Note: Docker mount host directory Docker access appears cannot open directory.: Permission denied

Solution: add one more parameter-- privileged=true-after mounting the directory

At this point, the study on "what is the Docker container data volume" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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