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 docker data Volume volume

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you the "sample Analysis of docker data Volume volume", which is easy to understand and well organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of docker data Volume volume".

1. What is the data volume volume

In order to understand what Docker Volume is, we first need to be clear about how the file system within Docker works. Docker images are stored in a series of read-only layers. When we open a container, Docker reads the read-only image and adds a read-write layer at the top. If the running container modifies an existing file, the file is copied from the underlying read-only layer to the top-most read-write layer. The old version of the file in the read-write layer is hidden under the file, but it is not unbroken-it still exists under the mirror. When the Docker container is deleted and then the mirror is restarted, a new container with no changes will be opened-those changes will be lost. The combination of this read-only layer and the read-write layer at the top is called Union File System (federated file system) by Docker.

In order to preserve (persist) data and share data between containers, Docker put forward the concept of Volumes. Quite simply, volumes are directories (or files), which are external default federated file systems or directories and files that normally exist in the host file system.

two。 Why use data volume volume

Docker mirrors are composed of a series of read-only tiers. When a container is started, Docker loads all read-only layers of the image and adds a read-write layer to the top layer. This design enables Docker to improve the efficiency of image construction, storage and distribution, saving time and storage space, but there are also the following problems.

(1) the files in the container exist in complex forms on the host, so it is not convenient to access the files in the container on the host.

(2) data cannot be shared among multiple containers

(3) when the container is deleted, the data generated by the container will be lost.

In order to solve these problems, Docker introduces the data volume (volume) mechanism. A volume is a specific file or folder that exists in one or more containers, this directory can exist in the host independently of the federated file system, and facilitates the sharing and persistence of data.

(1) volume is initialized when the container is created, and the files in it can be used when the container is running

(2) volume can be shared and reused among different containers

(3) the operation on the data in volume will take effect immediately

(4) data manipulation in volume will not affect the image itself.

(5) the life cycle of volume is independent of the life cycle of the container. Even if the container is deleted, the volume will still exist, and the volume used without any container will not be deleted by Docker.

3. How to use data volumes

3.1Mount volume (- v / path) from the container

When creating a new container using docker run, you can use the-v tag to add the data volume volume to the container. The following usage is to create a volume from a folder in the container. If the folder specified in the container does not exist, it will be generated automatically.

In the above concept, it is said that the host should have a folder bound to the volume mount point in the container. Where is the folder on the default host? use the docker inspect command to view the container details (CRT causes a SSH terminal)

[root@localhost ~] # docker inspect volume-test01 [{"Id": "81a74152e6f45a3f780ac7cdc37c9a089814f9a70aad1d27747093ca3c3dae3e", "Created": "2016-08-25T07:48:55.942949334Z", "Path": "/ bin/bash", "Args": [], "State": {"Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false "Pid": 10199, "ExitCode": 0, "Error": ", StartedAt": "2016-08-25T07:48:56.777918888Z", "FinishedAt": "0001-01-01T00:00:00Z"}, "Image": "sha256:4fd21defa24c8c07b3689b267a63d563ca0e26ef931b329fc3f3d46efb5bba2d", "ResolvConfPath": "/ var/lib/docker/containers/81a74152e6f45a3f780ac7cdc37c9a089814f9a70aad1d27747093ca3c3dae3e/resolv.conf", "HostnamePath": "/ var/lib/docker/containers/81a74152e6f45a3f780ac7cdc37c9a089814f9a70aad1d27747093ca3c3dae3e/hostname" "HostsPath": "/ var/lib/docker/containers/81a74152e6f45a3f780ac7cdc37c9a089814f9a70aad1d27747093ca3c3dae3e/hosts", "LogPath": "," Name ":" / volume-test01 "," RestartCount ": 0," Driver ":" devicemapper "," MountLabel ":" system_u:object_r:svirt_sandbox_file_t:s0:c47,c332 "," ProcessLabel ":" system_u:system_r:svirt_lxc_net_t:s0:c47,c332 " "AppArmorProfile": "," ExecIDs ": null," HostConfig ": {" Binds ": null," ContainerIDFile ":"," LogConfig ": {" Type ":" journald "," Config ": {}}," NetworkMode ":" default "," PortBindings ": {}," RestartPolicy ": {" Name ":" no "," MaximumRetryCount ": 0} "VolumeDriver": "," VolumesFrom ": null," CapAdd ": null," CapDrop ": null," Dns ": []," DnsOptions ": []," DnsSearch ": []," ExtraHosts ": null," GroupAdd ": null," IpcMode ":"," Links ": null," OomScoreAdj ": 0," PidMode ":", "Privileged": false, "PublishAllPorts": false: "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "," ShmSize ": 67108864," ConsoleSize ": [0,0]," Isolation ":", "CpuShares": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": null, "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null "CpuPeriod": 0, "CpuQuota": 0, "CpusetCpus": "," CpusetMems ":", "Devices": [], "KernelMemory": 0, "Memory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness":-1, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null} "GraphDriver": {"Name": "devicemapper", "Data": {"DeviceId": "29", "DeviceName": "docker-253:0-101330881-9cb32851050b1707022b475489686582a272d883a56a8ff52f3344f56b65639f", "DeviceSize": "10737418240"}}, "Mounts": [{"Name": "5ddd734c53a38a78a9f739157c63074b4aff736d4045925616d7753402304137", "Source": "/ var/lib/docker/volumes/5ddd734c53a38a78a9f739157c63074b4aff736d4045925616d7753402304137/_data" "Destination": "/ opt/vol-data", "Driver": "local", "Mode": "," RW ": true," Propagation ":"}]," Config ": {" Hostname ":" 81a74152e6f4 "," Domainname ":"," User ":"," AttachStdin ": true," AttachStdout ": true," AttachStderr ": true "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": null, "Cmd": ["/ bin/bash"], "Image": "test/mycentos:v1.0", "Volumes": {"/ opt/vol-data": {}}, "WorkingDir": "," Entrypoint ": null," OnBuild ": null "Labels": {}}, "NetworkSettings": {"Bridge": "," SandboxID ":" c73841812aab480cf8e6a02071e36951dceb002d7b36f7dc0b38ccd9db833ba5 "," HairpinMode ": false," LinkLocalIPv6Address ":", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/ var/run/docker/netns/c73841812aab", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "3e492b611d9bd2c202c8a6c8fe4b4a755393545348b1aeb4e60995609dabb07c" "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": {"bridge": {"IPAMConfig": null, "Links": null "Aliases": null, "NetworkID": "54001aaff29a231c9b2fe83459805d99dce0c21d6e2719f9b11bc90d8fe2f9c9", "EndpointID": "3e492b611d9bd2c202c8a6c8fe4b4a755393545348b1aeb4e60995609dabb07c", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "," GlobalIPv6PrefixLen ": 0 "MacAddress": "02:42:ac:11:00:02"}}]

Note that the Mounts node (the version of Docker uses 1.10.3), the use of data volumes is similar to mount directories or files under Linux.

"Mounts": [{"Name": "5ddd734c53a38a78a9f739157c63074b4aff736d4045925616d7753402304137", "Source": "/ var/lib/docker/volumes/5ddd734c53a38a78a9f739157c63074b4aff736d4045925616d7753402304137/_data", "Destination": "/ opt/vol-data", "Driver": "local", "Mode": "", "RW": true, "Propagation": ""}]

When we operate on the volume of the container, will the corresponding files on the host change accordingly? To test it, create a file test.txt on the volume of the container, and then see if the host will also exist synchronously.

After testing, when the volume on the container changes, the host will also change, and vice versa? It's the same after testing. Whether the container mount point changes or the host operates on the mount directory, one party will change accordingly.

Use docker commit to generate a new image, and then docker run-it runs the new image and finds that there are no files in the container mount directory. Indicates that the mount file is not saved when a new image is generated.

3.2 Mount volume (- v / host-path:/container-path) from the host

When mounting a host's files or folders as volume, you can add multiple volume to the container with multiple-v tags, or you can use: ro to specify that the volume is read-only. Note: if the same file exists in the mount directory specified in the container, it will be overwritten by the host

/ opt/vol-01 and / opt/vol-02 mount points are established on the host, corresponding to / opt/vol-test-1 and / opt/vol-test-2 in the container, respectively. The former has the permission to read and write by default, while the latter can only read with docker inspect.

When you create a new operation on the vol-test-2 of the container, you are prompted for read-only. On the host, the two mount points are added, modified, and deleted with OK, but neither of them works in the container. The application is supposed to be the second vol-test-2. I don't know what went wrong.

Use docker commit to generate a new image, and then docker run-it runs the new image and finds that there are no files in the container mount directory. Indicates that the mount file is not saved when a new image is generated.

3.3 add volume using Dockerfile

Use the VOLUME directive to add volume to the container

VOLUME / data

Multiple-time VOLUME ["/ data1", "/ data2"]

This situation is the same as in the first case, docker run-v / data. Note that using volume in dockerfile cannot mount the folder specified in the host as in the second method. At this time, in order to ensure the portability of Dockerfile, because there is no guarantee that all hosts have corresponding folders.

It is important to note that after using the VOLUME directive in Dockerfile, if you try to modify the volume, none of these modification instructions will take effect, such as the following example, try to add a file, modify the file and change all permissions of the file

FROM test/mycent:v1.0 RUN useradd foo VOLUME / data RUN touch / data/x RUN chown-R foo:foo / data

After creating the image through the Dockerfile and starting the container, the user foo exists in the container, and you can see the volume mounted in / data, but the owner of the / data folder has not been changed to foo, and there is no / data/x file under / data. But if the order is reversed, create the file first, authorize it first, and then mount the volume, you get the desired result.

4. Shared volume/ data volume container (--volumes-from)

If you have some continuously updated data to share between containers, it is best to create a data volume container. A data volume container is actually a normal container designed to provide data volumes for other containers to mount.

First create a data volume container named dbdata, which is specially mounted by other containers.

A file db.properties is created under / opt/dbdata.

Start two more containers, both a--volumes-from dbdata,b-volumes-from a.

You can see that there are db.properties files under / opt/dbdata of the vol_a,vol_b container. When you create the file vol_b.txt under / opt/dbdata of vol_b, the container dbdata,vol_a is also synchronized.

When you use the command docker inspect on dbdata,vol_a,vol_b, it is found that the Source under their Mounts is the same, indicating that they are all bound to the same directory of the host, so when the mount of one container is modified, other containers see the same effect.

If you have more than one mount source, you can use multiple-- volumes-from, as shown in

Docker run-it-name vol_use-volumes-from vol_a-volumes-from vol_b test/mycentos:v1.0 / bin/bash.

If a container is hung on a volume, the volume still exists even if the container stops running, and other containers can still continue-- volumes-from it.

5. Delete volume

If volume is mounted when the container is created

The corresponding files will be generated under / var/lib/docker/volumes/04b003b21b873157433deffbaf08bb0c89d234d3ec3c6576fdd7b61f5d41163e/_data (path, version, operating system varies, you can use docker inspect to view container details). When deleting a container, the directory on the host will not be deleted when mounting the directory, and the directory name is random and has no meaning, so when deleting the container, you need to properly handle the container's volume. There are 2 ways to delete volume when you delete a container

(1) docker rm-v delete the container. When you delete the container, add-v

(2) docker run-rm. Add-- rm when you start the container, then the container and the volume mounted by the container will be automatically deleted when the container stops running.

The container dbdata,vol_a,vol_b,vol_c has been created above, now use docker rm-v to see how it works.

Vol_c is-- volumes-from dbdata. When the vol_c is deleted, the mount directory of the host still exists and is not deleted. It is guessed that this is because other containers are created when they are connected or dbdata-v. Now try to delete the dbdata container (vol_a is-- volumes-from dbdata, vol_a has not been deleted, verify whether dbdata can be deleted).

When it is found that the dbdata is deleted, the host directory still exists, and it also shows that even if the vol_a is-- volumes-from dbdata and the vol_a has not been deleted, it does not matter. At the same time, it also indicates that as long as there is a container mounting the host directory, the host directory will not be deleted. Next, delete all the containers and look at the results.

As you can see, when the last container is deleted, the host volume directory is finally deleted.

6. Back up, restore, or migrate volume

It has been tested that when you use docker commit and other means to generate a new image, and then start the image to generate a new container, when the files in the original volume directory are missing and can generate a new image, the files under the volume are not packaged together to generate an image.

As the carrier of data, volume needs to back up, migrate, or restore the existing data in many cases. An easy way to think of is to use the docker inspect command to find the corresponding directory location on the volume corresponding host, and then copy the contents or package it with tar. When these clumsy practices are not recommended, because looking for folders on the host is prone to error and is not suitable for automated script execution.

You can use the following methods to back up volume

Docker run-rm-volumes-from dbdata-v $(pwd): / backup test/mycentos:v1.0 tar cvf / back/data.tar / data

This command starts a temporary container that mounts two volume. The first volume is shared with the volume to be backed up, and the second volume mounts the current directory (or absolute path) of the host to the container's / backup. After the container is running, the container (/ data) to be backed up is backed up to / backup/data.tar, and then the container is deleted, and the backed up data.tar is left in the current directory. Verify the operation.

Unexpectedly, he made a mistake, saying that he had no authority. When entering Container-it, docker randomly generates a user. As for how to authorize that user, we will study it later.

The above is all the content of the article "sample Analysis of docker data Volume volume". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Servers

Wechat

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

12
Report