問題:
在docker中啓動mysql容器,在mysql容器中對數據庫的更改(如創建數據庫,更改數據等),在commit後再次進入容器發現之前的更改全部沒有保存,如:
步驟如下:
1.後臺運行mysql容器,設置容器名稱爲mysql:
[root@localhost ~]# docker run --name=mysql -p 3306:3306 -d owenchen1992/mysql
f80791a0daf194fdba94f16a9d89ebec8ba8fbd8af28d3ea8b599b9d705f85ba
2.進入容器bash和mysql,創建一個數據庫TEST_DB,並驗證TEST_DB創建成功:
[root@localhost ~]# docker exec -it mysql bash
root@f80791a0daf1:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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.00 sec)
mysql> create database TEST_DB;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| TEST_DB |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
3.退出容器,並commit更改到鏡像:
mysql> exit
Bye
root@f80791a0daf1:/# exit
exit
[root@localhost ~]# docker commit mysql owenchen1992/mysql
sha256:8beaeefdd1c3d23981f5d2d69089f998ae1dbfec0e023005df07031477011fe6
4.重新啓動容器並進入bash和mysql,發現之前創建的TEST_DB不見了,說明之前的更改無效:
[root@localhost ~]# docker container stop mysql
mysql
[root@localhost ~]# docker container rm mysql
mysql
[root@localhost ~]# docker run --name=mysql -p 3306:3306 -d owenchen1992/mysql
a1a1b4174caaadda0ec4b01b9fe5f92d6b3464d85284042274f71aebde0915dd
[root@localhost ~]# docker exec -it mysql bash
root@a1a1b4174caa:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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.00 sec)
問題原因:
在mysql的Dockerfile中有這樣一行:
VOLUME /var/lib/mysql
這意味在容器中的目錄/var/lib/mysql的所有修改會對應到宿主機的某個位置,可以通過命令查看具體對應的宿主機目錄:docker inspect containerID/name. 當運行docker commit時,容器中的/var/lib/mysql目錄的更改並不會提交到鏡像中,但這些更改是隨時與宿主機對應的目錄同步的。當重新啓動commit後的鏡像時,container會重新在宿主機中創建一個目錄來保存其數據更新,因此並不是原先的宿主機目錄,所以新開啓的容器看不到之前的數據更改。
解決方法:
我們已經知道了問題發生的原因,就不難解決這個問題了,步驟如下:
1.找到mysql容器對應的宿主機目錄"/var/lib/docker/volumes/8496bbf33782bdadc027cdcf23197e5ebc36d11deb69ee833d63b557b3a7183d/_data":
[root@localhost ~]# docker inspect mysql
[
......
"Source": "/var/lib/docker/volumes/8496bbf33782bdadc027cdcf23197e5ebc36d11deb69ee833d63b557b3a7183d/_data",
"Destination": "/var/lib/mysql",
......
]
2.在mysql中創建一個數據庫TEST_DB:
[root@localhost ~]# docker exec -it mysql bash
root@a1a1b4174caa:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> create database TEST_DB;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| TEST_DB |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
3.退出容器,停止容器,刪除容器(不需要docker commit更新鏡像):
mysql> exit
Bye
root@a1a1b4174caa:/# exit
exit
[root@localhost ~]# docker container stop mysql
mysql
[root@localhost ~]# docker container rm mysql
mysql
4.後臺運行容器,並將宿主機對應的目錄掛載到容器的/var/lib/mysql目錄下並開啓讀寫權限(關鍵步驟):
[root@localhost ~]# docker run --name=mysql -p 3306:3306 -v /var/lib/docker/volumes/8496bbf33782bdadc027cdcf23197e5ebc36d11deb69ee833d63b557b3a7183d/_data:/var/lib/mysql:rw -d owenchen1992/mysql
5.此時進入新開啓的容器,並查看數據庫:
[root@localhost ~]# docker exec -it mysql bash
root@c7a9c8415654:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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 |
| TEST_DB |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
6.發現剛纔創建的數據庫TEST_DB沒有因爲重新運行容器而消失,問題解決!