############## 1, 启动一个容器 ############## [root1@c7-docker ~]# docker run --name t1 -d -e MYSQL_ROOT_PASSWORD=123456 -v mysql-test-dir:/var/lib/mysql/ -p 3308:3306 mysql:5.5
53f2a3c53d4c6769897ee484c3d2ff4e3f82ea1e390d4950b2162c08af05d7cc
[root1@c7-docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53f2a3c53d4c mysql:5.5 "docker-entrypoint.s…"3 seconds ago Up 3 seconds 0.0.0.0:3308->3306/tcp t1
[root1@c7-docker ~]# mysql -uroot -p123456 -P3308 --protocol tcp
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62 MySQL Community Server (GPL)
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema || mysql || performance_schema |
+--------------------+
3 rows inset(0.00 sec)
MySQL [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> use test;
Database changed
MySQL [test]> create table t1(id int);
Query OK, 0 rows affected (0.00 sec)
MySQL [test]> insert into t1 values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
MySQL [test]>select * from t1;
+------+
|id|
+------+
|1||2||3|
+------+
3 rows inset(0.00 sec)############## 2, 提交当前容器环境--保存为新的镜像 ############## [root1@c7-docker ~]# docker commit -m mysql_test_chag-port_img t1 mysql_port
sha256:9f90be3c200c6a8b535478375c4caf86d589d231afae1293d28026f9248f8ae3
[root1@c7-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql_port latest 9f90be3c200c 3 seconds ago 205MB
mysql 5.5 d404d78aa797 18 months ago 205MB
############## 3, 停止旧容器, 启动新容器 ############## [root1@c7-docker ~]# docker stop t1
t1
[root1@c7-docker ~]# docker run --name t2 -d -v mysql-test-dir:/var/lib/mysql/ -p 3309:3306 mysql_port
fd7cefe55db8bb2687ab044ff1473f0c935ac5ce867df81b3b13f570e9026868
[root1@c7-docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd7cefe55db8 mysql_port "docker-entrypoint.s…"3 seconds ago Up 2 seconds 0.0.0.0:3309->3306/tcp t2
[root1@c7-docker ~]# mysql -uroot -p123456 -P3309 --protocol tcp
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.62 MySQL Community Server (GPL)
MySQL [(none)]> show databases;
MySQL [(none)]> use test;
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 [test]>select * from t1;
+------+
|id|
+------+
|1||2||3|
+------+
3 rows inset(0.00 sec)