In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of Docker data volume management. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Data in Docker can be stored in media similar to virtual machine disks, called data volumes (Data Volume) in Docker. Data volumes can be used to store data from Docker applications and to share data among Docker containers.
The data volume is presented to the Docker container in the form of a directory that supports sharing among multiple containers, and modifications do not affect the image. Data volumes that use Docker are similar to mounting a file system in a system using mount.
In this section, we need to complete the following tasks in turn:
1. Create a data volume
two。 Manage data volume permissions
3. Mount the host file
4. Use data volume containers to share data
5. Data volume backup
I. create a data volume
The command docker run we learned in the container management experiment is used to create a container. You can add the-v parameter when you use the modified command to create and mount one or more data volumes to the currently running container. The function of-v is to mount a directory of the host to the container as the data volume of the container, so that a directory can be shared between the host and the container. If the local path does not exist, Docker will be created automatically.
In this lab, we mount 2 data volumes to the newly created container:
# create two directories mkdir / tmp/data1 / tmp/data2 # mount two directories to the newly created container docker run-t-I-- name shiyanlou-v / tmp/data1:/data1-v / tmp/data2:/data2 ubuntu / bin/bash
The-v parameter in the above command can be used multiple times and hung in multiple data volumes into the container. In the following parameter information, the colon is preceded by the host's local directory, and followed by the mount directory in the container.
Use docker inspect shiyanlou to view the volume information in the shiyanlou container:
# docker inspect shiyanlou
After entering the container, we can view and use the container volume, try to write data to the container volume, and then check whether it exists in the host:
You can see that the data volume mounted in the container has writeable permissions, so how to manage the permissions of the data volume? For example, how to create a read-only data volume?
2. Data volume permissions
The mounted data volume defaults to read-write permission. Unless there are special restrictions on the external file system, it can also be executed as read-only permission in docker run:
# create a data volume directory mkdir / tmp/readonlydata# to mount read-only to the shiyanlouro container docker run-t-I-- name shiyanlouro-v / tmp/readonlydata:/rodata:ro ubuntu / bin/bash
The parameters in the above command are very simple. Ro represents readonly, and the mounted data volume has read-only permission. At this time, we try to write to the data volume again:
In addition to being able to mount directories, files can also be mounted into containers as data volumes.
Third, mount the files on the host
In this lab, we want all containers to share the / etc/apt/sources.list of the host, so that all containers can be affected simply by changing the apt source of the host.
The copy code is as follows:
Docker run-t-I-- name shiyanloufile-v / etc/apt/sources.list:/etc/apt/sources.list:ro ubuntu / bin/bash
What if we want to share a data volume with multiple containers? for example, imagine a scenario where two applications that deal with uploaded data are running in different containers, but need to read files in the same folder at the same time. The best way is to use a data volume container.
4. Data volume container
If you need to share data among multiple containers and want to keep it permanently, the best way is to use a data volume container, similar to a NFS server that provides network file sharing services.
The data volume container is created in the same way as a normal container. You only need to specify a folder of the host as the data volume, and use the docker create command to create but not start the data volume container:
Docker create-v / shiyanloudata-- name shiyanloudb ubuntu / bin/true
Other containers that use this data volume container need to be created using the-- volumes-from parameter to specify the container name or ID:
Docker run-- volumes-from shiyanloudb...
Create site1 and site2 containers to mount the data volume container shiyanloudb:
You can connect to these two containers to manipulate the data volumes and see if they already have shared files with each other:
5. Back up data volumes
Continue to use the environment of experiment 4, we back up the data in the data volume container, backup method:
1. Create a new container
two。 Mount the data volume container
3. Mount the host local directory as a data volume
4. Back up the contents of the data volume container to the data volume mounted in the host local directory
5. Destroy the container after completing the backup operation
Follow the steps above to back up the data in the data volume container shiyanloudb:
# create backup directory mkdir / tmp/backup# create backup container docker run-- rm-- volumes-from shiyanloudb-v / tmp/backup:/backup ubuntu tar cvf / backup/shiyanloudb.tar / shiyanloudata
This is the end of this article on "sample Analysis of Docker data Volume Management". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.