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 Docker data Volume

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

Share

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

In this article, the editor introduces in detail "how to use Docker data volumes". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use Docker data volumes" can help you solve your doubts.

First, the 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, and the mounted data volumes can be displayed.

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 in the container's / data directory and under this directory. [root@sta2 data] # cd / var/lib/docker/volumes/b98191464fb0b1a888507b1e5b324802012297342adfe5d6125bcbfd08b621a9/_ data [root @ sta2 _ data] # touch b

View it in the container / data directory:

The / data # lsa bb 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 II, 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.

After reading this, the article "how to use Docker data volumes" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are 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

Development

Wechat

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

12
Report