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

The method of managing containers data by Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article focuses on "how Docker manages containers data". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Docker's method of managing containers data".

For Docker, containers manages data in two ways:

Data volumes

Data volume containers

Data volumes

A data volume refers to a special directory in container that has the following characteristics:

Initialization: initializes when the container is created. If the image to which container belongs has data at the mount point of the data volume, then the data is copied to the data volume at initialization.

Data volumes can be shared between Container and can be reused.

The data modification in the data volume is immediate, that is, once it is modified, the containers to which the data volume is mounted is visible in real time.

When you update image, the data changes in the data volume are not updated.

"even if container is deleted, its data volumes are not deleted."

Add a data volume

Add the parameter-v when you execute the command docker create or docker run to add a data volume for a container, and you can use-v multiple times to add multiple volumes. A simple example is as follows:

Docker run-d-P-- name web-v / webapp training/webapp python app.py

The above command creates a mount point at / webapp in container.

If you want to know the directory of / webapp on the host, execute:

Docker inspect web

The results are as follows:

... "Volumes": {"/ webapp": "/ var/lib/docker/vfs/dir/b0518f7a863879aa391da6e1d0c8455db1b0d7d6f716f49463952ebd558bbe1b"}, "VolumesRW": {"/ webapp": true}.

"Volumes" is the host directory of / webapp. "VolumesRW" indicates that the data volume can be read and written.

We found that the host directory of / webapp has a randomly generated name, which is not friendly, so when creating a container, we can specify that a directory on the host should be mounted to the specified directory of container as a data volume, just specify it when using the-v parameter:

Docker run-d-P-- name web-v / src/webapp:/opt/webapp training/webapp python app.py

As above, mount the / src/webapp of the host to the / opt/webapp of container. If / src/webapp does not exist, it will be created. The data volume is read and written by default. If you need to restrict it to be readable, execute as follows:

Docker run-d-P-- name web-v / src/webapp:/opt/webapp:ro training/webapp python app.py

Of course, you can also mount a separate file:

Docker run-- rm-it-v ~ / .bash_history:/.bash_history ubuntu / bin/bash

As above, all commands used in container are recorded, and when you leave container, you can still view the history.

Creating and mounting a data volume container

If you need to share persistent data between containers, or use persistent data from non-persistent containers, consider creating a data volume container and mounting the data to another containers.

Create the data volume containers as follows:

Docker create-v / data-- name dbdata training/postgres / bin/true

Container dbdata does not execute any applications.

You can mount / data to another containers using the parameter-volumes-from:

Docker run-d-volumes-from dbdata-- name db1 training/postgresdocker run-d-- volumes-from dbdata-- name db2 training/postgres

Db1 and db2 share data.

Data volume mount is transitive, and / data can be mounted to another container via db1 or db2:

Docker run-d-- name db3-- volumes-from db1 training/postgres

If you delete dbdata, db1, and db2 at this time, the data volume will not be deleted. Be sure to delete the last container mounted to the data volume, such as:

Docker rm-v db3 so far, I believe you have a deeper understanding of "Docker's method of managing containers data". 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