廢話很少說直接操做
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沒有被容器使用而回收。在容器中,添加或修改這個文件夾裏的文件也不會影響容器的聯合文件系統。