内容目录
Docker容器备份与迁移
1.前言
最近想把自己一年前创建的图床打包备份迁移到另外一台服务器,总结一下迁移的方法。
2.备份容器镜像
想要迁移容器,我们先把目前的容器打包成镜像。
我们首先确认一下目前的镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nmtan/chevereto 1.4.1 1e17e86b38af 16 months ago 522MB
输入docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
找到我们目前容器的名字
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
CONTAINER ID NAMES STATUS
cb06ac6ac523 nginx-app-1 Up 9 months
d2a19f2a44ac chevereto-app-1 Up 3 hours
e08750f6ac76 chevereto-db-1 Up 3 hours
我们要备份chevereto-app-1这个容器可以输入下面命令导出一个新的新的镜像
docker commit chevereto-app-1 mychevereto
进行备份
docker save -o ./mychevereto.tar mychevereto
当然你也可以上传到docker hub
docker login
docker tag mychevereto tutu/chevereto:1.4.1
docker push tutu/chevereto:1.4.1
3.恢复容器
##删除容器
docker stop chevereto-app-1
docker rm chevereto-app-1
##删除镜像
docker images
docker rmi nmtan/chevereto
##导入容器镜像
docker load -i mychevereto.tar
导入之后可以吧映射过的目录直接打包过来放到对应的位置,然后可以使用docker-compose来定义容器镜像和各种变量来达到目标。