Docker persistent storage, Data Volume

After the docker container is restarted, we can modify and delete it and so on.
If it is a container of a database, the data in it does not want to disappear as the container disappears. It requires persistent data storage.

Data Volume  

 

 

 This is the Dockerfile of mysql on the docker hub. The VOLUME here means that the generated data is written to / var / lib / mysql on the current host.

 

[miller@docker4 ~]$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
daocloud.io/library/mysql          latest              9228ee8bac7a        11 days ago         547MB
[miller@docker4 ~]$ docker run -d --name=mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true daocloud.io/library/mysql
e7945f20bc456038a110a9798299356c8e7912a22eceaaf4e2bd29880460ab6f
-e MYSQL_ALLOW_EMPTY_PASSWORD = true tells mysql to use it without a password.

https://hub.docker.com/_/mysql Here is a detailed explanation of some parameters. You can also set a password.

[docker4 Miller @ ~ ] $ # Docker Volume LS will find a place to store data on the host 
DRIVER NAME VOLUME 
local 283f5d6584642ae6d32d5e02fd1330855b501dd891ddf38c5b40428183c652c8 # redis this is a 
local 330b65bda64c22b0929443d9eaa2db28c3a2468f76876b706c8efdbcbf6e4919 # This is the mysql

 

[miller@docker4 docker]$ docker volume inspect 330b65bda64c22b0929443d9eaa2db28c3a2468f76876b706c8efdbcbf6e4919
[
    {
        "CreatedAt": "2020-04-11T20:26:36+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/330b65bda64c22b0929443d9eaa2db28c3a2468f76876b706c8efdbcbf6e4919/_data",
        "Name": "330b65bda64c22b0929443d9eaa2db28c3a2468f76876b706c8efdbcbf6e4919",
        "Options": null,
        "Scope": "local"
    }
]

"Mountpoint": "/ var / lib / docker / volumes / 330b65bda64c22b0929443d9eaa2db28c3a2468f76876b706c8efdbcbf6e4919 / _data" This path is 
the file path on the host where docker is installed . The data generated by the database will be stored here. The volume will not disappear due to the deletion of the container.

 

Since the name of the volume is not friendly, you can define it yourself: alias:

[miller@docker4 ~]$ docker run -d --name=mysql1 -v mysql:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true daocloud.io/library/mysql
7b006082d7d9c1cc8f17dfaefda26afea6d2adb0c89b216eafe193fa9420c917
-v mysql: / var / lib / mysql on this parameter. Volume [/ var / lib / mysql] in the root mysql Dockerfile will do the same.
[miller @ docker4 ~ ] $ docker volume ls 
DRIVER VOLUME NAME 
local 330b65bda64c22b0929443d9eaa2db28c3a2468f76876b706c8efdbcbf6e4919 # redis 
local mysql # Volume of the mysql container just created

 

You can go to the MySQL container and log in to the client.

[miller@docker4 ~]$ docker exec  -it mysql1 /bin/bash
root@c057b8fbb3ad:/# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

In this database, create a library, then stop the mysql container, and then delete the container.

[miller @ docker4 ~ ] $ docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
c057b8fbb3ad daocloud.io / library / mysql    " docker-entrypoint.s… "    4 minutes ago Up 4 minutes         3306 / tcp, 33060 / tcp mysql1 
[miller @ docker4 ~] $ docker rm- f mysql1 # stop this container and delete 
mysql1
[miller @ docker4 ~ ] $ docker volume ls 
DRIVER VOLUME NAME 
local 330b65bda64c22b0929443d9eaa2db28c3a2468f76876b706c8efdbcbf6e4919 
local mysql # volume is still there. Create a container later and still use this volume

# As you can see, create a container again. If you still use the same volume. You can continue to use the original data. There is no loss. 

[miller@docker4 ~]$ docker run -d --name=mysql2 -v mysql:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true daocloud.io/library/mysql
f9cfe55e7e6d85a6c44bc177fb324051e5735e80dbf30c5224cbfd0c7f844181
[miller@docker4 ~]$ docker exec -it mysql2 mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. 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.01 sec)

 

Guess you like

Origin www.cnblogs.com/chengege/p/12682501.html
Recommended