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

How to use data Volume in Docker

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces how to use the data volume in Docker. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

one。 Use of data volumes

Sometimes you need to use a database, but you want its data to be saved locally. Docker provides data volumes for you to easily manipulate the data. A data volume is a special directory that can be used by one or more containers. It bypasses UFS and provides many useful features:

Data volumes can be shared and reused between containers

Changes to the data volume will take effect immediately

Updates to data volumes do not affect mirroring

The data volume will always exist by default, even if the container is deleted

Note: the use of data volumes is similar to mount directories or files under Linux. The files in the directory specified as the mount point in the image will be hidden to show that you are looking at the mounted data volume.

This experimental environment: Tencent Cloud server CentOS 6.7x86x64

Add a data volume:

# docker run-d-it-- name busybox-v / data/ busybox

This creates a / data directory within the container and loads a data volume into the container's / data directory.

Enter the container:

# docker exec-it busybox sh

View the directory mapping:

# docker inspect-f {{.volumes}} busybox map [/ data:/var/lib/docker/volumes/b98191464fb0b1a888507b1e5b324802012297342adfe5d6125bcbfd08b621a9/_data]

You can see that the / data directory in the container is mapped to the / var/lib/docker/volumes/b98191464fb0b1a888507b1e5b324802012297342adfe5d6125bcbfd08b621a9/_data directory, so the data is synchronized between the / data directory in the container and this directory.

[root@sta2 data] # cd / var/lib/docker/volumes/b98191464fb0b1a888507b1e5b324802012297342adfe5d6125bcbfd08b621a9/_ data [root @ sta2 _ data] # touch b

View it in the container / data directory:

/ data # ls a b

B file still exists.

Data volumes are designed to persist data, its life cycle is independent of containers, Docker does not automatically delete data volumes after the container is deleted, and there is no mechanism such as garbage collection to deal with data volumes that are not referenced by any containers. If you need to remove the data volume while deleting the container. You can use the docker rm-v command when deleting a container.

[root@sta2 docker] # docker stop eec30d8d6fce [root@sta2 docker] # docker rm-v eec30d8d6fce

The-v flag can also be used to specify that the directory of a local host is mounted to the container, and the-v flag can also mount a single file from the host to the container

[root@sta2 docker] # docker run-it-- name mybusybox-v / data:/data busybox sh

This approach is equivalent to specifying the directory to be mapped locally and loading the local data volume / data directory into the / data directory in the container.

/ # cd / data//data # lsa/data # touch c

Then check the local / data directory to see if the c file exists:

[root@sta2 data] # ls a c

The default permission for Docker to mount data volumes is read and write, and users can also specify read-only through: ro.

# docker run-it-- name mybusybox-v / data:/data:ro busybox sh

two。 Data volume container

If you have some continuously updated data to share between containers, it is best to create a data volume container.

[root@sta2 data] # docker run-d-v / data/-- name dbdata busybox # first, create a data volume container called dbdata

Then, use-volumes-from in other containers to mount the data volumes in the dbdata container.

# docker run-d-volumes-from dbdata-- name db1 nginx# docker run-d-- volumes-from dbdata-- name db2 nginx

You can also use-volumes-from to mount multiple volumes from multiple containers:

# docker run-d-name db3-volumes-from db1-volumes-from db nginx

Tip: containers of data volumes mounted with the-volumes-from parameter do not need to remain running by themselves.

Backup data Volum

First use the-volumes-from flag to create a container that loads the dbdata container volume and mount the current directory from the host to the container's / backup directory. The command is as follows:

# docker run-- volumes-from dbdata-v / data:/backup busybox tar cvf / backup/backup.tar.gz / data

Restore

If you want to recover data to a container, first create a container dbdata2 with empty data volumes.

# docker run-v / dbdata-- name dbdata2 ubuntu / bin/bash

Then create another container, mount the data volume in the dbdata2 container volume, and use untar to extract the backup file into the mounted container volume.

# docker run-- volumes-from dbdata2-v / data:/backup busybox tar xvf / backup/backup.tar.gz

To view / verify the recovered data, you can start another container to mount the same container volume to view

# docker run-- volumes-from dbdata2 busybox / bin/ls / dbdata

Delete data

If the mounted containers (db1 and db2) are deleted, the data volume is not automatically deleted. If you want to delete a data volume, you must use the docker rm-v command to delete the associated container when you delete the last container that still holds it.

The above is all the contents of the article "how to use data volumes in Docker". Thank you for reading! Hope to share the content to help you, more related 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: 293

*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