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 the operation method of Docker container data volume

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the operation method of Docker container data volume". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Container data volume describes what a data volume is

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

It is hoped that data will be shared between containers.

If the data generated by the Docker container does not generate a new image through docker commit so that the data can be saved and downloaded 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.

What can data volumes do?

Persistence of containers

Inherit and share data between containers

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

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

Characteristics

Data volumes can be shared or reused between containers

Changes in the volume can take effect directly

Changes in the data volume are not included in the update of the mirror

The life cycle of a data volume lasts until no container uses it.

Docker container data volume operation direct command to add data volume

Command format

Docker run-it-v absolute path of host: absolute path in container image name

Case demonstration

# 1. File sharing between the container and the host [root@docker] # docker run-itd-v / volume:/containervolume centos:latestc0eb80a4425eac21b933ce1a81132eb08eeaaf30ac5adb077d44c11dd7260268#2, and the directory [root@docker ~] # ll-d / volumedrwxr-xr-x is automatically created under the / of the host and the container. 2 root root 6 November 1 23:25 / volume [root@docker] # docker exec-it $(docker ps-Q) ls-ld / containervolumedrwxr-xr-x. 2 root root 6 Nov 1 15:25 / containervolume#3, verify, create the test file under the containervolume directory in the container, and find that the host volume directory shares the test file [root@docker ~] # docker exec-it $(docker ps-Q) touch / containervolume/test [root@docker ~] # ll / volume-rw-r--r--. 1 root root 0 November 1 23:29 test#4, after exiting the container, the host modifies the volume file, starts the container again, and finds that the nidaye file dockerfile adds the data volume under the dataVolumeContainer directory in the container

Dockerfile description

You can use the VOLUME directive in Dockerfile to add one or more data volumes to the mirror

VOLUME ["/ dataVolumeContainer", "/ dataVolumeContainer2", "/ data/VolumeContainer3"]

For portability and sharing, using-v host directory: container directory cannot be implemented directly in Dockerfile. Since the host directory depends on a specific host, there is no guarantee that such a specific directory exists on all hosts.

Case demonstration

# 1. Create a mydocker folder under the / directory and enter [root@docker ~] # mkdir / mydocker#2, Dockerfile builds [root@docker ~] # vim / mydocker/dockerfile FROM centos:latest # the image created comes from the parent image centos VOLUME ["/ volume"] # create data volume CMD echo "create---volume----successl" # print data volume created successfully CMD / bin/bash#3, Build generates a new image volume/centos [root@docker ~] # docker build-f / mydocker/dockerfile-t volume/centos. Sending build context to Docker daemon 519.2MB Step 1 create---volume----successl 4: FROM centos:latest-- > 0f3e07c0138f Step 2 Running in 9dd2d3fbd75c Removing intermediate container 9dd2d3fbd75c 4: VOLUME ["/ volume"]-- > Running in 0059989ef83c Removing intermediate container 0059989ef83c-- > 9a7f6ae84e23 Step 3 Running in 9dd2d3fbd75c Removing intermediate container 9dd2d3fbd75c 4: CMD echo "create---volume----successl"-- > Running in 9dd2d3fbd75c Removing intermediate container 9dd2d3fbd75c-- -> 6f8a83b9feca Step 4 bin/bash 4: CMD / bin/bash-- > Running in 18edd4a6af38 Removing intermediate container 18edd4a6af38-- > 30b4d769d4e8 Successfully built 30b4d769d4e8 Successfully tagged volume/centos:latest # Image created successfully # 4, Use the new image to start the container [root@docker ~] # docker run-itd volume/centos 53d070c79a13dd4ecfe3208a3907cd00e4dc3b1a45c2b890d642cb96b7e05217#5, verify The directory volume [root@docker ~] # docker exec-it $(docker ps-Q) ls-ld / volume drwxr-xr-x is created successfully in the container. 2 root root 6 Nov 1 16:03 / volume#6, verify, create a file test on the host, and the container / volume directory successfully shares data [root@docker ~] # touch / var/lib/docker/volumes/53d070c79a13dd4ecfe3208a3907cd00e4dc3b1a45c2b890d642cb96b7e05217/_data/test # host data volume default mount directory [root@docker ~] # docker exec-it $(docker ps-Q) ls-l / volume-rw-r--r--. 1 root root 0 Nov 1 16:12 testDocker data volume container brief introduction to data volume container

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

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

Case demonstration

Start a centos_1 container and mount the data volume

[root@docker ~] # docker run-itd-- name= "centos_1"-v / volume:/volumecontainer_1 centos 9f73357be614f7c5098b2ef3a83534002992ee2f281b8b984ed7af5c03a7e29b# host / volume create file test [root@docker ~] # touch / volume/test# verification, container centos_1 Share the file test [root@docker ~] # docker exec-it $(docker ps-Q) ls-l / volumecontainer_1 total 0-rw-r--r--. under / volume/container_1/ 1 root root 0 Nov 2 14:33 test

Start a centos_2 container and inherit the container centos_1 file

[root@docker ~] # docker run-itd-- name= "centos_2"-- volumes-from centos_1 centos baefc86b170c376745761aaea1abc0843521b272213337109510170ff1ccb304# verification: container centos_2 inherits the container centos_1 file [root@docker ~] # docker exec-it $(docker ps-n 1-Q) ls-l / volumecontainer_1 total 0-rw-r--r--. 1 root root 0 Nov 2 14:33 test# verification: add files to container centos_2, host and container centos_1 can share [root@docker ~] # docker exec-it $(docker ps-n 1-Q) touch / volumecontainer_1/test_centos_2# host successfully share files test_container_2 [root@docker ~] # ls-l / volume/ total usage 0-rw-r--r--. 1 root root 0 November 2 22:33 test-rw-r--r--. 1 root root 0 November 2 22:42 test_centos_2# container centos_1 successfully shares files test_container_2 [root@docker ~] # docker exec-it 9f73357be614 ls-l / volumecontainer_1 total 0-rw-r--r--. 1 root root 0 Nov 2 14:33 test-rw-r--r--. 1 root root 0 Nov 2 14:42 test_centos_2 "how to operate Docker Container data volumes" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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