数据持久化之Data Volume
废话不多说直接操作
1.启动一个MySQL测试容器
[root@localhost labs]# docker pull mysql #下载MySQL镜像 [root@localhost labs]# docker run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql #启动一个测试容器,注意 MYSQL_ALLOW_EMPTY_PASSWORD 参数为使用空密码,需要使用true显示设置
root@localhost labs]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ea6b8b8988a mysql "docker-entrypoint..." 4 seconds ago Up 3 seconds 3306/tcp, 33060/tcp mysql1
2.查看MySQL volume信息(在MySQL dockerfile中定义了VOLUME /var/lib/mysql)
[root@localhost labs]# docker volume ls #查看当前主机上docker volume
DRIVER VOLUME NAME
local 454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf4b
[root@localhost labs]# docker volume inspect 454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf4b
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf #在宿主机上的路径
"Name": "454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf4b",
"Options": {},
"Scope": "local"
}
]
3.再创建一个MySQL测试容器并观察volume信息
[root@localhost labs]# docker run -d --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
c228c2caa839d51f9da356912dc56d3689f43ab8efecb97edb65f5557fb5ef02
[root@localhost labs]#
[root@localhost labs]# docker volume ls
DRIVER VOLUME NAME
local 454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf4b
local 5c10a74e6798a503aa0c7a76412c7d27ab17e1e817843057b19c2acb2829c87b
[root@localhost labs]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORT
c228c2caa839 mysql "docker-entrypoint..." 5 seconds ago Up 4 seconds 3306
6ea6b8b8988a mysql "docker-entrypoint..." 57 seconds ago Up 56 seconds 3306
[root@localhost labs]# docker volume inspect 5c10a74e6798a503aa0c7a76412c7d27ab17e1e817843057b19c2acb2829c87b
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/5c10a74e6798a503aa0c7a76412c7d27ab17e1e817843057b19c2acb2829c8
"Name": "5c10a74e6798a503aa0c7a76412c7d27ab17e1e817843057b19c2acb2829c87b",
"Options": {},
"Scope": "local"
}
]
4.删除刚刚创建的两个测试容器,查看volume是否还在
[root@localhost labs]# docker rm -f mysql1
mysql1
[root@localhost labs]# docker rm -f mysql2
mysql2
[root@localhost labs]# docker volume ls #在容器被删除时,volume并不会被删除
DRIVER VOLUME NAME
local 454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf4b
local 5c10a74e6798a503aa0c7a76412c7d27ab17e1e817843057b19c2acb2829c87b
5.删除所有volume
[root@localhost labs]# docker volume rm 454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf4b 5c1017e1e817843057b19c2acb2829c87b
454e122fc14e377e8328dbd66fb20751ccf6c0cf55988956bcee20dd2e1fcf4b
5c10a74e6798a503aa0c7a76412c7d27ab17e1e817843057b19c2acb2829c87b
[root@localhost labs]# docker volume ls
DRIVER VOLUME NAME
6.使用-v参数绑定一个volume,并查看volume信息
[root@localhost labs]# docker run -d --name mysql1 -v mysql:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true #使用-v 参数绑定。/var/lib/mysql值于docker VOLUME的值一致
fe98466f9854c14657aa4b2139c060283c965a82e67c436f8a072b6961d0770d
[root@localhost labs]# docker volume ls
DRIVER VOLUME NAME
local mysql
[root@localhost labs]# docker volume inspect mysql
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/mysql/_data", #可以看到挂载路径名称变为了我们指定的名称
"Name": "mysql",
"Options": {},
"Scope": "local"
}
]
7.进入mysql1容器内部,新建一个库名称为docker,然后退出容器并删除容器
[root@localhost labs]# docker exec -it mysql1 /bin/bash
root@fe98466f9854:/# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database docker;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| docker |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit
Bye
root@fe98466f9854:/# exit
exit
[root@localhost labs]# docker rm -f mysql1
mysql1
[root@localhost labs]# docker ps -a
8.查看volume是否还存在
[root@localhost labs]# docker volume ls
DRIVER VOLUME NAME
local mysql
9.新建一个mysql2测试容器,并绑定mysql volume
[root@localhost labs]# docker run -d --name mysql2 -v mysql:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
29398d828164f046f7f605cfa32028079aab241bcf4fda0f11c4889cbc71036b
[root@localhost labs]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29398d828164 mysql "docker-entrypoint..." 3 seconds ago Up 2 seconds 3306/tcp, 33060/tcp mysql2
10.进入mysql2容器,查看在mysql1中的库
[root@localhost labs]# docker exec -it mysql2 /bin/bash
root@29398d828164:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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 | #mysql1上新建的docker库mysql2挂载后依然存在
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
总结:
1.在Dockerfile中使用VOLUME关键字可以为镜像定义一个volume卷,在使用镜像创建容器时会自动创建一个,名称默认为一串UUID,使用-v参数则目录名是volume名称。volume里的数据都存储在这个子目录的_data目录下。 2.Docker Volume能让容器从宿主主机中读取文件或持久化数据到宿主主机内,让容器与容器产生的数据分离开来。你可以简单地把它理解为linux服务器上的挂载点。一个容器可以挂载多个不同的目录。Volume的生命周期是独立于容器的生命周期之外的,即使容器删除了,volume也会被保留下来,Docker也不会因为这个volume没有被容器使用而回收。在容器中,添加或修改这个文件夹里的文件也不会影响容器的联合文件系统。
"一劳永逸" 的话,有是有的,而 "一劳永逸" 的事却极少