09 Docker container data volume command -v mount

content

Docker container data volume

introduce

To sum up in one sentence: persistence and synchronization of containers! Data can also be shared between containers

Method 1: Local mount Use the command to mount -v

The container stops. At this time, Linux modifies the file. When the docker container starts, it will update the past. 

Example:

Test: (Can the local link be successful)

​ ​

Benefit: We only need to modify it locally, and the container will be automatically synchronized!

Suppose we delete the container

It is found that the data volume we mounted to the local is still not lost, which realizes the container data persistence function!

Named mounts and anonymous mounts

Named mount: 

Anonymous mount (recommended):

 docker volume ls View the list of mount names

 docker volume inspect mount name to view the mount status

docker directory /var/lib/docker/

We can easily find one of our volumes through named mounts. In most cases, named mounts are used.

Distinguish three mounting methods

expand:

View volume mounted path docekr inspect

data volume container --volumes-from

test:

Multiple mysql realize data sharing

in conclusion:


Docker container data volume

introduce

We will have such a requirement that we need some directories in the container to be synchronized to the local. There are two purposes for this:

First: For some containers, we often need to modify the configuration files frequently, and it is troublesome to enter the container frequently.

Second: For the data in the container (mysql), we hope that it can be persisted locally as a backup.

For the above two requirements, docker provides me with a data volume technology, we can mount the directory in the docker container with the local Linux directory through the data volume (two-way binding) . In addition to the above-mentioned data mounting between the machine and the container, the container and the container can also be mounted.

 data? If the data is all in the container, then we delete the container and the data will be lost! Requirement: Data can be persisted

MySQL, the container is deleted, the database is deleted and run away! Requirement: MySQL data can be stored locally!

There can be a data sharing technology between containers! The data generated in the Docker container is synchronized to the local!

This is the roll technique! Directory mount, mount the directory in our container to Linux!

To sum up in one sentence: persistence and synchronization of containers! Data can also be shared between containers

Method 1: Local mount Use the command to mount -v

The container stops. At this time, Linux modifies the file. When the docker container starts, it will update the past. 

docker run -it -v 主机目录:容器目录
#测试
[root@master ~]# doceke run -it -v /home/ceshi:/home centos bin/bash

#启动起来时候我们可以通过 docker inspect 容器id  来查看详细信息

Example :

        MySQL data persistence problem ( 需要数据持久化的需要思考) data

        Next, we mount the /etc/mysql/conf.dand /var/lib/mysqldirectory in mysql to the local /home/mysql/configand /home/mysql/datadirectory.

# 1、启动(挂载)   #安装mysql时需要配置密码  需要注意
[root@gh ~]# docker run --name mysql-test01 -itd -p 3306:3306 -v /home/mysql/config:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
cac43dce9d011ee0cda595361e9915cc1b64ac2a5d5aab57fa9db3327a23e03a

    -p 端口映射
    -v 卷挂载
    -e 环境配置
    --name 容器名字

Check if the mount is successful docker inspect   to view the details of the container

[root@gh ~]# docker inspect cac43dce9d01

# 未挂载前
[root@gh home]# cd /home
[root@gh home]# ls
gh  www

# 挂载 后
[root@gh home]# ls
gh  mysql  www
[root@gh home]# cd mysql

# 发现多出两个目录 config  data 
[root@gh mysql]# ls
config  data 
[root@gh mysql]# cd config
[root@gh config]# ls
[root@gh config]# cd ..

# 发现data目录下 的文件确实是/var/lib/mysql目录下的东西
[root@gh mysql]# cd data/
[root@gh data]# ls
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  public_key.pem   server-key.pem
[root@gh data]# 

Test: (Can the local link be successful)

 

Benefit: We only need to modify it locally, and the container will be automatically synchronized!

Suppose we delete the container

It is found that the data volume we mounted to the local is still not lost, which realizes the container data persistence function!

Named mounts and anonymous mounts

The mounting method we mentioned above is the specified path mounting (we specified the mounting path of the host). We can also mount in two ways, named and anonymous. We do not need to specify the mount path of the host for both named and anonymous mounts.

Named mount

        docker run -d -P --name nginx01 -v /etc/nginx nginx

# 匿名挂载
-v 容器内路径!
$ docker run -d -P --name nginx01 -v /etc/nginx nginx

# 查看所有的volume(卷)的情况
$ docker volume ls    
DRIVER              VOLUME NAME # 容器内的卷名(匿名卷挂载)
local     264e5fc71bbad5a329b28300f35713f4882546d95b91740a89cc882819843aa3
local     867bf5dfd84b154d0cb262b69c8284b7c88cd43bf6bc0d0f05bd35f6d0f07fd0
local     d355f4d2c1d8ff7d0baeb9147552e5d730b8201993e7dee5805c71b7b25ab6ec
local     e6aa6b909e1139822136e5232ee1ba0932bc308afaa6cbcac6458966cb80be15
        
# 这里发现,这种就是匿名挂载,我们在 -v只写了容器内的路径,没有写容器外的路径!

Anonymous mount (recommended) :

        docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx

        -v juming-nginx:/etc/nginx named a name not a directory  

        -v juming-nginx:/etc/nginx added /   without adding  /  at the beginning of the absolute directory is a normal configuration

# 具名挂载 -P:表示随机映射端口
$ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
9663cfcb1e5a9a1548867481bfddab9fd7824a6dc4c778bf438a040fe891f0ee

# 查看所有的volume(卷)的情况
$ docker volume ls                  
DRIVER    VOLUME NAME
local     264e5fc71bbad5a329b28300f35713f4882546d95b91740a89cc882819843aa3
local     867bf5dfd84b154d0cb262b69c8284b7c88cd43bf6bc0d0f05bd35f6d0f07fd0
local     d355f4d2c1d8ff7d0baeb9147552e5d730b8201993e7dee5805c71b7b25ab6ec
local     e6aa6b909e1139822136e5232ee1ba0932bc308afaa6cbcac6458966cb80be15
local     juming-nginx #多了一个名字


# 通过 -v 卷名:查看容器内路径
# 查看一下这个卷
$ docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2022-03-23T13:55:34+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data", #默认目录
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]

 docker volume ls View a list of mount names

 docker volume inspect 挂载名 View mount information

docker directory  /var/lib/docker/

All volumes in the docker container, if there is no directory specified, are in /var/lib/docker/ volumes/ xxx_data

# docekr 目录 /var/lib/docker
[root@gh ~]# cd /var/lib/docker/
[root@gh docker]# ls
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
[root@gh docker]# cd volumes/
[root@gh volumes]# ls
264e5fc71bbad5a329b28300f35713f4882546d95b91740a89cc882819843aa3  d355f4d2c1d8ff7d0baeb9147552e5d730b8201993e7dee5805c71b7b25ab6ec
867bf5dfd84b154d0cb262b69c8284b7c88cd43bf6bc0d0f05bd35f6d0f07fd0  e6aa6b909e1139822136e5232ee1ba0932bc308afaa6cbcac6458966cb80be15
backingFsBlockDev                                                 metadata.db
[root@gh volumes]# 

We can easily find one of our volumes through named mounts. In most cases, we use named mounts

Distinguish three mounting methods

#区分三种挂载方式

# 三种挂载: 匿名挂载、具名挂载、指定路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载 docker volume ls 是查看不到的

expand:

# 通过 -v 容器内路径: ro rw 改变读写权限
ro #readonly 只读
rw #readwrite 可读可写

#一旦这个设置了容器权限,容器对我们挂载出来的内容就有了限定了
docker run -d -P --name nginx05 -v juming:/etc/nginx:ro nginx
docker run -d -P --name nginx05 -v juming:/etc/nginx:rw nginx

# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作!

View volume mounted path docekr inspect

data volume container --volumes-from

        Synchronize data between multiple containers

# 启动docker 01
[root@gh ~]# docker run -it  --name docker01 cacef2eba361 /bin/bash
[root@f90a7cb23452 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar 21 04:01 dev
drwxr-xr-x   1 root root 4096 Mar 21 04:01 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 108 root root    0 Mar 21 04:01 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Mar 21 04:01 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar 21 04:01 volume01
drwxr-xr-x   2 root root 4096 Mar 21 04:01 volume02

# 挂载上 docker02 volumes-form
[root@gh ~]# docker run -it  --name docker02 --volumes-from docker01 cacef2eba361 /bin/bash
[root@173c46baf306 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar 21 04:02 dev
drwxr-xr-x   1 root root 4096 Mar 21 04:02 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 110 root root    0 Mar 21 04:02 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Mar 21 04:02 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar 21 04:01 volume01
drwxr-xr-x   2 root root 4096 Mar 21 04:01 volume02


# 进入docker 01   attacth
[root@gh /]# docker attach f90a7cb23452
[root@f90a7cb23452 /]# 

# 在docker01 的 volume01 中创建一个文件 docker01-test
[root@173c46baf306 /]# cd volume01
[root@173c46baf306 volume01]# touch docker01-test


# 在docker 02 的容器中查看 vilume01 发现有 docker01 创建的文件  
[root@f90a7cb23452 volume01]# ls
docker01-test
docker 01 创建的数据同步到了docker02上面

test:

Test: You can delete docekr01 and see if docker02 and docker03 can still access this file. The
test can still be accessed

Multiple mysql realize data sharing

[root@gh~]# docker run -d -p 3310:3306 -v/etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7


[root@gh~]# docker run -d -p 3310:3306  -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 
--volumes-from mysql01 mysql:5.7

#这个时候,可以实现连个容器数据同步

in conclusion:

The transfer of configuration information between containers, the life cycle of the data volume container continues until no container is used.

But once you persist to the local, at this time, the local data will not be deleted!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324052597&siteId=291194637