Storage and sharing of Docker data

Posted by TwistedLogix on Sat, 19 Feb 2022 10:21:17 +0100

an introduction to

Generally speaking, there are two modes of Docker data persistence:

1, volume based on local file system

  • Docker automatic creation
  • Bind the mounted volume and create it yourself

2, plugin based volume

1 based on local file system

1.1 Docker automatic creation

1.1.1 create a MySQL image, and then run the container

sudo docker run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql

Running mysql image
Name the running container mysql1
The environment variable is MYSQL_ALLOW_EMPTY_PASSWORD=true

[vagrant@docker-node1 ~]$ sudo docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS                 NAMES
61437fae2c96   mysql     "docker-entrypoint.s..."   3 minutes ago   Up 3 minutes               3306/tcp, 33060/tcp   mysql1
9cca42e69ec3   nginx     "/docker-entrypoint...."   23 hours ago    Exited (0) 7 h

mysql is already running

[vagrant@docker-node1 ~]$ sudo docker volume ls
DRIVER    VOLUME NAME
local     29a72721b3e4c96a93795c7188669753c1ddac37e4e22140f009e06886836d8f

1.1.2 query volume information according to volume name

[vagrant@docker-node1 ~]$ sudo docker volume inspect 29a72721b3e4c96a93795c7188669753c1ddac37e4e22140f009e06886836d8f
[
    {
        "CreatedAt": "2021-04-22T21:21:32Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/29a72721b3e4c96a93795c7188669753c1ddac37e4e22140f009e06886836d8f/_data",
        "Name": "29a72721b3e4c96a93795c7188669753c1ddac37e4e22140f009e06886836d8f",
        "Options": null,
        "Scope": "local"
    }
]

Mountpoint shows that the volume is mounted in the local / var/lib/docker/volumes /

1.1.3 create a second mysql container

[vagrant@docker-node1 ~]$ sudo docker run -d --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true  mysql

1.1.4 view the volume of the second container

[vagrant@docker-node1 ~]$ sudo docker volume ls
DRIVER    VOLUME NAME
local     29a72721b3e4c96a93795c7188669753c1ddac37e4e22140f009e06886836d8f
local     fd711cc812cc88877ec680e1363dc69015cac6600ef1e6ba06320d57983afc21

1.1.5 query the volume information of the second container

[vagrant@docker-node1 ~]$ sudo docker volume inspect fd711cc812cc88877ec680e1363dc69015cac6600ef1e6ba06320d57983afc21
[
    {
        "CreatedAt": "2021-04-22T21:30:46Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/fd711cc812cc88877ec680e1363dc69015cac6600ef1e6ba06320d57983afc21/_data",
        "Name": "fd711cc812cc88877ec680e1363dc69015cac6600ef1e6ba06320d57983afc21",
        "Options": null,
        "Scope": "local"
    }
]
[vagrant@docker-node1 ~]$ 

1.1.6 test: after deleting the container, the corresponding volume still exists

1.1.6.1 delete container

[vagrant@docker-node1 ~]$ sudo docker stop mysql1 mysql2
mysql1
mysql2
[vagrant@docker-node1 ~]$ sudo docker rm mysql1 mysql2
mysql1
mysql2
[vagrant@docker-node1 ~]$ sudo docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[vagrant@docker-node1 ~]$ 

1.1.6.2 query volume

[vagrant@docker-node1 ~]$ sudo docker volume ls
DRIVER    VOLUME NAME
local     29a72721b3e4c96a93795c7188669753c1ddac37e4e22140f009e06886836d8f
local     fd711cc812cc88877ec680e1363dc69015cac6600ef1e6ba06320d57983afc21

volume is still there

1.2 name volume to facilitate management

1.2.1 recreate the container and name the volume

[vagrant@docker-node1 ~]$ sudo docker run -d -v mysql:/var/lib/mysql --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true  mysql

Add - v parameter
-v mysql:var/lib/mysql indicates that the volume is named mysql The content in the corresponding container is the storage path of volume

1.2.2 querying volume information

[vagrant@docker-node1 ~]$ sudo docker volume ls
DRIVER    VOLUME NAME
local     mysql

1.2.3 verify whether the Volume is effective

1.2.3.1 enter the container of mysql2

[vagrant@docker-node1 ~]$ sudo docker exec -it mysql1 /bin/bash
root@6af0ee16ce56:/# 

1.2.3.2 enter mysql shell

[vagrant@docker-node1 ~]$ sudo docker exec -it mysql1 /bin/bash
root@6af0ee16ce56:/# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

1.2.3.3 query databases in shell

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

Now there are four lines

mysql> create database docker;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| docker             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit

Successfully added docker

1.2.3.4 then delete the container mysql1

[vagrant@docker-node1 ~]$ sudo docker rm -f mysql1

1.2.3.5 query volume again

[vagrant@docker-node1 ~]$ sudo docker volume ls
DRIVER    VOLUME NAME
local     mysql

volume still exists, but it is not deleted together

1.2.4 test 2: change the name of the container without changing the name of the volume

1.2.4.1 re create a new container: mysql2

[vagrant@docker-node1 ~]$ sudo docker run -d -v mysql:/var/lib/mysql --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true  mysql
cbd62573de5a0be5b736eef7ea9bfb0f865cb443a6f274a0d5d3d6dbe52eea6c

1.2.4.2 enter mysql2 shell and query databases

[vagrant@docker-node1 ~]$ sudo docker exec -it mysql2 /bin/bash
root@cbd62573de5a:/# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| docker             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.07 sec)

mysql> 

You can see that docker is still inside

1.2.5 test 3: do not change the container name, but change the volume name to mysql_new

[vagrant@docker-node1 ~]$ sudo docker volume ls
DRIVER    VOLUME NAME
local     mysql
local     mysql_new

There will be two volume s

[vagrant@docker-node1 ~]$ sudo docker exec -it mysql1 /bin/bash
root@eab0252ec6d9:/# mysql    
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

There is no docker here

1.3 summary

thus it can be seen:

  1. Delete the container, and the volume is still in
  2. Even if the old / new container is named mysql1 / mysql2, as long as the naming of - v mysql:/var/lib/mysql is consistent Share a data volume
  3. However, even if the old / new container names are consistent with mysql1 / mysql1, as long as -v mysql names are inconsistent, there are two data volume s Data is kept in different volumes

2. Bind mounting (local and container synchronization data)

2.1 command to put volume into the specified path

docker run -v /home/aaa:/root/aaa

-v is a mapping Map home/aaa to root/aaa
/Home / AAA: / root / AAA indicates that the specified directory of the container is mapped locally

2.2 web page synchronization test

2.2.1 create Dockerfile and create image based on Dockerfile

[vagrant@docker-node1 docker-nginx]$ sudo docker build -t xu1102/xu-nginx .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM nginx:latest
 ---> 62d49f9bab67
Step 2/3 : WORKDIR /usr/share/nginx/html
 ---> Running in 7a969bf6e460
Removing intermediate container 7a969bf6e460
 ---> ce3e35bfe763
Step 3/3 : COPY index.html index.html
 ---> 6861bf6b0127
Successfully built 6861bf6b0127
Successfully tagged yahui1102/xu-nginx:latest
[vagrant@docker-node1 docker-nginx]$ sudo docker image ls
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yahui1102/xu-nginx   latest    6861bf6b0127   4 minutes ago   133MB

2.2.2 create container based on image

[vagrant@docker-node1 docker-nginx]$ sudo docker run -d -p 80:80 --name web xu1102/xu-nginx
16cb71428fff68466732819ffd78807db71e609c21c4a8151ed846ba95e305b9
[vagrant@docker-node1 docker-nginx]$ sudo docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS         PORTS                             NAMES
16cb71428fff   yahui1102/xu-nginx   "/docker-entrypoint...."   10 seconds ago   Up 8 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web

docker run -d: indicates that the container is running in the background
-p 80:80: mapping port 80 of container to port 80 of docker host
– name web: indicates that the running container is named web

2.2.3 curl local ip 127.0.0.1 in container

[vagrant@docker-node1 docker-nginx]$ curl 127.0.0.1
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <title>hello</title>

</head>

<body>
  <h1>Hello Docker! </h1>
</body>
</html>
[vagrant@docker-node1 docker-nginx]$ more index.html 
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <title>hello</title>

</head>

<body>
  <h1>Hello Docker! </h1>
</body>
</html>
[vagrant@docker-node1 docker-nginx]$ 

You can see the address of the curl host and the html file in the container
Description the files in the container are synchronized to the virtual machine

2.2.4 test in the web page and enter eth1

2.3 synchronization test 2

2.3.1 forcibly delete the web container first

[vagrant@docker-node1 docker-nginx]$ docker rm -f web
web

2.3.2 running a new container and mapping ports

[vagrant@docker-node1 docker-nginx]$ docker run -d -v $(pwd):/usr/share/nginx/html -p 80:80 --name web yahui1102/xu-nginx
656b55d32e825f31bcebb44e81732f411ff07019d01fadade1490217628b24c0
[vagrant@docker-node1 docker-nginx]$ 

-v represents mapping
$(pwd):/usr/share/nginx/html indicates that the current path file is mapped to the html file

2.3.3 enter the container to check the information synchronization

[vagrant@docker-node1 docker-nginx]$ sudo docker exec -it web /bin/bash
root@656b55d32e82:/usr/share/nginx/html# ls
Dockerfile  index.html
root@656b55d32e82:/usr/share/nginx/html# touch test.txt
root@656b55d32e82:/usr/share/nginx/html# exit
exit
[vagrant@docker-node1 docker-nginx]$ ls
Dockerfile  index.html  test.txt
[vagrant@docker-node1 docker-nginx]$ sudo vim test.txt
[vagrant@docker-node1 docker-nginx]$ sudo docker exec -it web /bin/bash
root@656b55d32e82:/usr/share/nginx/html# more test.txt
hello world

Enter the container and see Dockerfile and index HTML has been synchronized
Create test. In the container Txt exit, there are more local test txt
Modify the test file 'hello world' locally
Enter the container and see that the test file also has the words' hello world '

Topics: Docker