参考
上一节我们在docker1上创建mysql容器,并使用了 Rex-Ray volume mysqldata ,更新了数据库内容。最后将容器销毁了。
本节我们将在docker2上创建一个mysql容器,使用上一节中的 Rex-Ray volume mysqldata
root@docker2:~# docker run --name mydb_on_docker2 -v mysqldata:/var/lib/mysql -d mysql
本次docker run 不需要指定passwd ,因为密码已经保存到mysqldata中了。上面命令执行成功后,Rex-Ray volume mysqldata自动挂载到了docker2上
root@docker2:~# docker volume inspect mysqldata
[
{
"CreatedAt": "0001-01-01T00:00:00Z",
"Driver": "rexray",
"Labels": null,
"Mountpoint": "",
"Name": "mysqldata",
"Options": null,
"Scope": "global",
"Status": {
"availabilityZone": "",
"fields": null,
"iops": 0,
"name": "mysqldata",
"server": "virtualbox",
"service": "virtualbox",
"size": 2,
"type": "HardDisk"
}
}
]
root@docker2:~# docker inspect mydb_on_docker2 | jq .[0].Mounts
[
{
"Type": "volume",
"Name": "mysqldata",
"Source": "",
"Destination": "/var/lib/mysql",
"Driver": "rexray",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
登录数据库查看在docker1上新建的表和数据是否还在
root@docker2:~# docker exec -it mydb_on_docker2 bash
root@d8c462a6e9e4:/# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.15 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from my_id;
+------+
| id |
+------+
| 111 |
+------+
1 row in set (0.00 sec)
mysql>
当前实验拓扑如下:
Rex-Ray 可以提供跨主机的volume,起生命周期不依赖 Docker Host 和容器。是stateful 容器理想的数据存储方式。
如何使用其他storage provider 的 volume driver ,部署和配合 storage provider 会有所不同,不过 Docker 在使用 volume 的方式是一样的:
1、通过 docker volume create --driver 创建volume
2、创建容器时使用 -v 指定上一步中创建的volume