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 are the ways of managing Docker data

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

Share

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

This article mainly talks about "what are the ways of Docker data management". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the ways of Docker data management.

Docker Foundation: data Management

In the process of using Docker, users often need to be able to view the data generated by applications in the container, or need to back up the data in the container, or even share the data among multiple containers, which inevitably involves the data management operation of the container. There are two main ways to manage data in a container: data volume (Data Volumes) and data volume container (Data Volume Containers).

Data volume

A data volume is a special directory that can be used by containers. It bypasses the file system and provides many useful features:

Data volumes can be shared and reused between containers.

Changes to the data volume take effect immediately.

Updates to the data volume do not affect mirroring.

The data volume will remain until there is no container in use.

5. Video detailed explanation

The use of data volumes is similar to the mount operation of directories or files under linux.

Create a data volume within the container

When using the docker run command, use the-v flag to create a data volume within the container. Use the-v flag multiple times to create multiple data volumes.

In the following example, we use the myimg/webapp image to create a web container and create a data volume to be mounted to the container's / webdata directory.

$sudo docker run-d-P-name web-v / webdata myimg/webapp python app.py

Mount a host directory as a data volume

You can also use the-v flag to specify that a local existing directory is mounted to the container as a data volume:

$sudo docker run-d-P-name web-v / var/data:/opt/webdata myimg/webapp python app.py

The above command mounts the host's / var/data directory to the container's / opt/webdata directory.

This feature is especially convenient when testing, for example, users can place some programs or data in the local directory and then use it in the container. In addition, the path to the local directory must be an absolute path, and if the directory does not exist, Docker will automatically create it.

The default permission for Docker to mount data volumes is read-write (rw), and users can also specify it as read-only through the ro tag:

$sudo docker run-d-P-name web-v / var/data:/opt/webdata:ro myimg/webapp python app.py

With the addition of ro, the data in the volume mounted in the container becomes read-only.

Mount a local host file as a data volume

The-v flag can also mount files from a host to a container as a data volume, but doing so can cause some problems. It is recommended that you still mount the file in the same directory.

Data volume container

If users need to share some continuously updated data between containers, the easiest way is to use data volume containers. A data volume container is actually an ordinary container that is specifically used to provide data volumes for other containers to mount. Here is a brief description of how to use it.

The first step is to create a data volume container mydata and mount a data volume to the / data directory in it.

$sudo docker run-it-v / data-- name mydata ubuntu

Then use-- volumes-from in other containers to mount the data volumes in the mydata container. For example, create two containers, mycon1 and mycon2, and mount the data volume from the mydata container:

Sudo docker run-it-- volumes-from mydata-- name mycon1 ubuntu$ sudo docker run-it-- volumes-from mydata-- name mycon2 ubuntu

(note that there is no information for the data volume specified in the command, which means that the directory where the data volume is mounted in the new container is the same as in the source container.)

At this point, both container mycon1 and mycon2 mount the same data volume to the same directory / data. Any of the three containers that write data in this directory can be seen by other containers. You can use the-- volumes-from parameter multiple times to mount multiple volumes from multiple containers. You can also mount data volumes from other containers that already have containers mounted. And the container of the data volume mounted with the-- volumes-from parameter does not need to remain running by itself.

However, when you delete a container with a data volume mounted, the data volume is not automatically deleted. If you want to delete a data volume, you must explicitly use the docker rm-v command to delete the associated container when deleting the last container that still holds it.

The use of data volume containers allows users to upgrade and move data volumes freely between containers, which are described in more detail below.

Using data volume container to migrate data

The data volume can be backed up and restored by the data volume container to realize the data migration.

Backup

Use the following command to back up the data volumes in the mydata data volume container:

$sudo docker run-- volumes-from mydata-v $(pwd): / backup-- name worker ubuntu tar cvf / backup/backup.tar / data

This command first creates a container worker using the Ubuntu image. Also use the-- volumes-from mydata parameter to have the worker container mount the data volume of the mydata container. Next, use the-v $(pwd): / backup parameter to mount the local current directory to the / backup directory of the worker container.

After the worker container starts, the command tar cvf / backup/backup.tar / data is used to back up the contents under / data as / backup/backup.tar in the container, that is, the backup.tar under the current directory of the host host.

Restore

If you want to restore data to a container, you can do the following. First create a container mydata2 with data volumes:

$sudo docker run-v / data-- name mydata2 ubuntu / bin/bash

Then create another new container, mount the data volume of mydata2, and use tar to extract the backup file to the mounted container volume:

$sudo docker run-- volumes-from mydata2-v $(pwd): / backup busybox tar xvf / backup/backup.tar here, I believe you have a better understanding of "what are the ways of Docker data management". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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

Servers

Wechat

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

12
Report