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

Storage volumes for getting started with Dcoker

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

Share

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

Storage volumes for getting started with Dcoker

Debut: arppinging.com

Storage volume 1 for getting started with Docker, file 2 in Docker, problems and solutions 1) problem 2) solution 3, what is a docker storage volume? 1) Overview 2) characteristics of volumes (volume) 3) types of volumes (volume types) 4, use docker storage volumes (volume) 1) docker-managed volume2) bind mount volume3) replicate volumes using other containers

Storage volumes for getting started with Docker I. Files in Docker

First of all, we need to review the previous content:

1. The docker image is made up of multiple layers. When you start the container, docker loads the read-only image layer and adds a read-write layer at the top of the image stack.

2. If the running container modifies an existing file, the file will be copied from the read-only layer to the read-write layer, and the read-only version of the file still exists, but it has been hidden by the copy of the file in the read-write layer, which is the "copy on write (COW)" mechanism.

Generally speaking, the container is layered, and the top layer is the read-write layer, and all changes to data are saved at the top layer; after running the container, all operations will not affect the image itself. If it is an editing operation, docker will only copy files from the read-only layer (not mobile) to the read-write layer for editing.

Files in docker II, problems and solutions 1) problems

1. When the user closes and restarts the container, the data in the container will not be affected, but if the container is deleted, all its data will be lost.

2. The data is stored in the federated file system and is not easily accessed by the host.

3. It is inconvenient to share data between containers.

4. It is inefficient for containers to write and change data, and some files need to be copied from the underlying layer, which is not suitable for applications with high IO.

2) solution

"Volume" is one or more "directories" of the container. This class directory can bypass the federated file system and "bind (associate)" to the directory on the host.

Directory binding 3. What is a docker storage volume? 1) Overview

Bind the host directory directly to the directory in the container. When the container writes data to the directory, it can directly write to the host directory. In this way, the data in the container can bypass the restrictions of the internal file system of the container, establish an association with the file system of the host, and achieve data sharing. When the container is stopped or deleted, the data will not be lost.

2) the characteristics of volume (volume)

1. Volume will be created when the container is initialized, and the data in the volume provided by base image will be copied during this period. (for example, if you bind / data/docker/b1 on the host machine and / etc/ in container b1, the contents of / etc/ in the container will be copied to the host / dta/docker/b1/ directory)

2. The original intention of volume is to persist data independent of the lifecycle of the container, so the volume will not be deleted or garbage collected even if the volume is not referenced when the container is deleted.

3. Volume provides a container-independent data management mechanism for docker.

(you can think of "mirror" as a static file, such as "program", and compare "volume" to dynamic content, such as "data". Therefore, the image can be reused and the volume can be shared; the volume realizes the separation of "program (mirror)" and "data (volume)", and the user no longer has to consider the environment where the container in which the image is running.

Docker Volume 3) Type of volume (volume types)

Docker has two types of volumes, each of which has a mount point in the container, but its location on the host is different.

3.1 bind mount volume (bind mount volumes)

A volume that points to a user-specified location on the host file system

3.2 docker-manged volume (docker manages volumes)

The docker daemon creates managed volumes in a portion of the host's file system that's system that's owned by docker

Two types of volumes

Bind mount volume: specify the path of the binding

Docker-managed volume: the path of the host is defined by docker daemon

Fourth, use docker storage volume (volume)

Regardless of the type of volume, the-v parameter of docker run is used.

1) docker-managed volume

1.1 create container b1 and specify the / data/ directory of b1 to use storage volumes

[root@localhost] # docker run-- name b1-it-v / data busybox

/ # cd / data/

/ data # ls

/ data #

1.2 View the associated host directory

[root@localhost b1] # docker inspect-f {{.Mounts}} b1

[{volume 4e1f3596768357e009da8df6d38c4339df04b64dee2d80e262609f83b87d4177/ var/lib/docker/volumes/4e1f3596768357e009da8df6d38c4339df04b64dee2d80e262609f83b87d4177/_data / data local true}]

[root@localhost b1] #

1.3 write files in the host directory

[root@localhost b1] # cd / var/lib/docker/volumes/4e1f3596768357e009da8df6d38c4339df04b64dee2d80e262609f83b87d4177/_data/

[root@localhost _ data] # echo 'welcome to arppinging.com' > > test.txt

[root@localhost _ data] # cat test.txt

Welcome to arppinging.com

[root@localhost _ data] #

1.4 check whether there is a test.txt file in container b1

/ data # cat / data/test.txt

Welcome to arppinging.com

/ data #

2) bind mount volume

When you use bind to mount a volume, if the host's directory does not exist, the directory is automatically created.

1.1 create container b2 and configure the / data/docker/b2/ of the host to associate with the / data/ of container b2

[root@localhost] # docker run-- name b2-it-v / data/docker/b2:/data busybox

/ # ls / data/

/ #

1.2 View b2's associated directory

[root@localhost _ data] # docker inspect-f {{.Mounts}} b2

[{bind / data/docker/b2 / data true rprivate}]

[root@localhost _ data] #

1.3 create a file in / data/ of b2

/ # cd / data/

/ data # echo 'welcome to b2.arppinging.com' > b2.html

/ data #

1.4 can I see the files created by b2 in the / data/docker/b2 of the host?

[root@localhost /] # cd / data/docker/b2/

[root@localhost b2] # ls

B2.html

[root@localhost b2] # cat b2.html

Welcome to b2.arppinging.com

[root@localhost b2] #

1.5 delete container b2 to see if the directory file of the host still exists

[root@localhost ~] # docker rm b2

B2

[root@localhost] # docker ps-a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Ec2ff6aeb48a busybox "sh" 10 minutes ago Exited (0) 4 minutes ago b1

[root@localhost ~] #

[root@localhost ~] # cd / data/docker/b2/

[root@localhost b2] # cat b2.html

Welcome to b2.arppinging.com

[root@localhost b2] #

3) copy volumes that use other containers

Copy and use volumes from other containers, using the-- volumes-from option

Create a container b3, using b1's storage volume

[root@localhost b2] # docker run-- name b3-it-- volumes-from b1 busybox

/ # cat / data/test.txt

Welcome to arppinging.com

/ #

View Mapping Volum

[root@localhost b2] # docker inspect-f {{.Mounts}} b1

[{volume 4e1f3596768357e009da8df6d38c4339df04b64dee2d80e262609f83b87d4177/ var/lib/docker/volumes/4e1f3596768357e009da8df6d38c4339df04b64dee2d80e262609f83b87d4177/_data / data local true}]

[root@localhost b2] # docker inspect-f {{.Mounts}} b3

[{volume 4e1f3596768357e009da8df6d38c4339df04b64dee2d80e262609f83b87d4177/ var/lib/docker/volumes/4e1f3596768357e009da8df6d38c4339df04b64dee2d80e262609f83b87d4177/_data / data local true}]

[root@localhost b2] #

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