在日常的 Docker 运维工作中,我们经常遇到以下两个场景
镜像名称太长或难记:拉取下来的镜像带有一长串私有仓库地址(如 registry.cn-hangzhou…),想要改成简短的名称(如 nginx:v1)。
离线环境部署:生产服务器无法连接互联网,或者网络环境较差,需要将本地开发环境构建好的镜像“搬运”到生产服务器上
镜像改名 打标签 1 [root@localhost ~]# docker tag registry.cn-hangzhou.aliyuncs.com/jiangxuliu/dockerhub:nginx-custom nginx:v1
验证结果 1 2 3 4 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID SIZE nginx v1 a3b1d2ab34ea 192MB registry.cn-hangzhou.aliyuncs.com/jiangxuliu/dockerhub nginx-custom a3b1d2ab34ea 192MB
镜像打包与迁移 在源机器上打包镜像 1 2 [root@localhost ~]# docker save -o my_project_images.tar nginx:v1 wordpress:v1
传输文件到目标机器 1 2 [root@localhost ~]# scp my_project_images.tar root@192.168.1.100:/root/
目标机器上加载镜像 1 2 [root@localhost ~]# docker load -i my_project_images.tar
结果验证 1 2 3 4 [root@target-server ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v1 a3b1d2ab34ea 2 days ago 192MB wordpress v1 b685e29c8d24 2 days ago 735MB