Docker容器迁移(WordPress+mysql为例)

本文介绍跨主机容器迁移,理论上适用于大部分Docker应用
由于Docker容器运行产生的数据并不是持久化的,一旦容器被删除其中数据同样被删除,生产环境下请使用-v参数挂载数据卷以持久化数据!!!
涉及到Mysql容器迁移,除下列方法,还可使用

1.使用mysqldump或mysqlhotcopy等工具进行数据库备份来进行数据库迁移操作
2.重新挂载原数据卷实现迁移

以迁移Wordpress及关联mysql容器为例

Docker ps查看当前容器状态,记录容器ID/容器名
docker ps

[root@MiWiFi-RC01-srv ~] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83d03d81ab2a mysql:5.7 "docker-entrypoint..." About an hour ago Up About an hour 33060/tcp, 0.0.0.0:3301->3306/tcp mysql00 795748985b7b wordpress "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp wordpress00 e1840165eb89 registry "/entrypoint.sh /e..." 8 days ago Up 2 days 0.0.0.0:5000->5000/tcp registry 

在这里插入图片描述

使用docker inspect查看容器信息,获取容器数据地址
docker inspect mysql00
打印出信息可能会很多,可使用grep搜索
docker inspect mysql00 | grep Source>

[root@MiWiFi-RC01-srv ~] "Source": "/var/lib/docker/volumes/80e6f32c9005f737ada7019dbabee846476b07c99ffdcc5f1fe752fc658bab66/_data", [root@MiWiFi-RC01-srv ~] "Source": "/var/lib/docker/volumes/e1a1d94fd2ef7510cdd4c17e18871e056bf34d76c45b817f3d60c47cc4375f56/_data", 

记录地址

使用docker commit由容器打包镜像
docker commit mysql00 mysqltest:v0
docker commit wordpress00 wordpresstest:v0>

[root@MiWiFi-RC01-srv ~] sha256:7a13c0ab60c208d097f8fcc63a7c32f6b4b7430b625506cfa7fd19cb9edeae3a [root@MiWiFi-RC01-srv ~] sha256:4deb7db2440a8f65e56c088d6be50e29d41fa5f7c96e89af112691722e97d7b2 

参数说明
docker commit 容器id 生成镜像名: [TAG]

保存镜像
docker save mysqltest -o mysqltest.tar
docker save wordpresstest -o wordpresstest.tar

[root@MiWiFi-RC01-srv ~] [root@MiWiFi-RC01-srv ~] 

参数说明
docker save 镜像名 -o 压缩包名.tar

可使用SCP迁移所需数据
scp -r /xxxx 用户名@IP:/xxx
下文使用Xshell下的xftp移动数据至另一台主机

查看打包好的镜像位置
pwd
ls
复制mysqltest.tar wordpresstest.tar 镜像至目标主机

[root@MiWiFi-RC01-srv ~] /root [root@MiWiFi-RC01-srv ~] anaconda-ks.cfg mysqltest.tar wordpresstest.tar 

进入容器数据目录
cd /var/lib/docker/volumes/80e6f32c9005f737ada7019dbabee846476b07c99ffdcc5f1fe752fc658bab66/
cd
/var/lib/docker/volumes/e1a1d94fd2ef7510cdd4c17e18871e056bf34d76c45b817f3d60c47cc4375f56/

[root@MiWiFi-RC01-srv ~] [root@MiWiFi-RC01-srv 80e6f32c9005f737ada7019dbabee846476b07c99ffdcc5f1fe752fc658bab66] _data [root@MiWiFi-RC01-srv ~] [root@MiWiFi-RC01-srv e1a1d94fd2ef7510cdd4c17e18871e056bf34d76c45b817f3d60c47cc4375f56] _data 

复制文件夹至目标主机/var/lib/docker/volumes/目录下

被迁移主机操作完成,进入迁移主机

进入目标主机,查看docker状态
docker ps

[root@docker01 ~] Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? [root@docker01 ~] [root@docker01 ~] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 

使用docker load 导入使用docker save导出的镜像
docker load -i wordpresstest.tar>
docker load -i mysqltest.tar

在这里插入图片描述
此次演示文件放置在docker文件夹下

[root@docker01 docker] b0064388b160: Loading layer [==================================================>] 7.68 kB/7.68 kB Loaded image: mysqltest:v0 [root@docker01 docker] 096b2fc5baea: Loading layer [==================================================>] 4.608 kB/4.608 kB Loaded image: wordpresstest:v0 

使用docker images查看本地镜像
docker images

[root@docker01 docker] REPOSITORY TAG IMAGE ID CREATED SIZE wordpresstest v0 4deb7db2440a About an hour ago 616 MB mysqltest v0 7a13c0ab60c2 About an hour ago 569 MB 

使用docker run拉起Wordpress与mysql容器
docker run -d -p 80:80 --name wordpresstest wordpresstest:v0
docker run -d -p 3301:3306 -e MYSQL_ROOT_PASSWORD=12345678 --name mysqltest mysqltest:v0

[root@docker01 docker] 3612dc9ba4a1d1045309717ec0e5891e8287fceef174bb5df620deaee58d1905 [root@docker01 docker] 863241b5271e77240a5fcaf75a0c145bcd2f898f36b9ab0da4290f6b05c6db44 

使用docker inspect查看新容器数据地址
docker inspect mysqltest | grep Source
docker inspect wordpresstest | grep Source

[root@docker01 docker] "Source": "/var/lib/docker/volumes/100ac33bd265d65a84def671f3388b48d340fe2fd7983f5f50cfdbe9b215e009/_data", [root@docker01 docker] "Source": "/var/lib/docker/volumes/cacf1f6f39f03f03fc56c22fa6a6a15bf7beb5a79af540589f3d1e1e0b914d07/_data", 

删除/var/lib/docker/volumes/100ac33bd265d65a84def671f3388b48d340fe2fd7983f5f50cfdbe9b215e009/_data
/var/lib/docker/volumes/cacf1f6f39f03f03fc56c22fa6a6a15bf7beb5a79af540589f3d1e1e0b914d07/_data
将原主机_data文件夹复制到迁移后容器目录
可使用rmcp命令操作

数据迁移完成后,使用docker restart重启容器
docker restart mysqltest
docker restart wordpresstest

[root@docker01 docker] mysqltest [root@docker01 docker] wordpresstest 

浏览器输入主机ip进入页面测试应用状态
提示Error establishing a database connection错误
在这里插入图片描述

修复mysql重建与wordpress的连接
进入mysql
docker exec -it mysqltest /bin/bash

[root@docker01 docker] bash-4.2 

登陆数据库
mysql -u root -p12345678

bash-4.2 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.42 MySQL Community Server (GPL) Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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> 

查看原wordpress库是否存在
show databases;

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | wordpress | +--------------------+ 5 rows in set (0.00 sec) 

原库已迁移完成

我们需要重建链接
进入wordpress表,查看wp_options表单option_name为siteurl行

mysql> use wordpress; 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 wp_options limit 1; +-----------+-------------+-----------------------+----------+ | option_id | option_name | option_value | autoload | +-----------+-------------+-----------------------+----------+ | 1 | siteurl | http://192.168.31.143 | yes | +-----------+-------------+-----------------------+----------+ 1 row in set (0.00 sec) 

更新为新host
更新语句update wp_options set option_value=replace(option_value,‘旧ip’,‘新ip’) where option_value LIKE ‘%旧ip%’;

mysql> update wp_options set option_value=replace(option_value,'192.168.31.143','192.168.31.98') where option_value LIKE '%192.168.31.143%'; Query OK, 2 rows affected (0.04 sec) Rows matched: 2 Changed: 2 Warnings: 0 

退出容器
重启mysql容器
docker start mysqltest

[root@docker01 ~] Error response from daemon: driver failed programming external connectivity on endpoint mysqltest (b10f9695221934c0999461c12e1fa54117b7ef857584b4fe78ab783748bde13f): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3301 -j DNAT --to-destination 172.17.0.3:3306 ! -i docker0: iptables: No chain/target/match by that name. (exit status 1)) Error: failed to start containers: mysqltest 

提示iptables failed这是由于docker启动后变更了firewalld导致的, iptables 规则与docker产生冲突

重新启动docker,重新生成自定义链即可
systemctl restart docker

[root@docker01 ~] [root@docker01 ~] mysqltest [root@docker01 ~] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 863241b5271e mysqltest:v0 "docker-entrypoint..." 20 hours ago Up 11 minutes 33060/tcp, 0.0.0.0:3301->3306/tcp mysqltest 3612dc9ba4a1 wordpresstest:v0 "docker-entrypoint..." 21 hours ago Up 21 seconds 0.0.0.0:80->80/tcp wordpresstest 

修改wordpress配置文件wp-config.php
使用find查找文件位置
find / -name wp-config.php
编辑wp-config.php
vim /var/lib/docker/volumes/cacf1f6f39f03f03fc56c22fa6a6a15bf7beb5a79af540589f3d1e1e0b914d07/_data/wp-config.php

[root@docker01 ~] /var/lib/docker/volumes/cacf1f6f39f03f03fc56c22fa6a6a15bf7beb5a79af540589f3d1e1e0b914d07/_data/wp-config.php [root@docker01 ~] 

找到DB_HOST,更新数据库地址

i键进入改写模式
esc退出改写模式
:wq保存

在这里插入图片描述

刷新页面,应用已迁移完成

在这里插入图片描述

原文链接:https://blog.csdn.net/weixin_43935695/article/details/131108641?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168994567316800180690880%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168994567316800180690880&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-2-131108641-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享