应用场景
1:把某个服务器上的镜像迁移到另一台服务器上,导入保存为文件,然后再进行恢复回一个镜像
2:对现有的运行的容器进行配置修改或升级之后,重新生成一个新的镜像,基于新的镜像创建为修改后的容器
实践
把运行了的容器打包保存为新的镜像
docker commit 容器 新的镜像名称
说明:
上一节中我们的部署来我们的后台网站地址把它放到容器内部,这种也算是对容器的修改,那现在我们需要直接把修改后的容器打包会心动的镜像,后续其他服务器可以直接的拉取镜像,然后运行容器即可
步骤:
- 1)打包新的镜像
提交修改
[root@localhost data]# docker commit mynginx newmynginx
sha256:cd903b9a327f6e622336dd1306001d26c7d398da6eb82a045afd8b10e2f443e6
查看当前镜像
[root@localhost data]# docker imsges
docker: 'imsges' is not a docker command.
See 'docker --help'
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newmynginx latest cd903b9a327f 11 seconds ago 146MB
tomcat latest 891fcd9c5b3a 2 weeks ago 647MB
nginx latest f35646e83998 3 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
centos centos7.1.1503 e1430271e2f9 20 months ago 212MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost data]#
-2 )测试新生成的镜像的运行
默认80端口已经被占用,所以修改修改映射的端口
[root@localhost data]# docker run -di --name mynxgin_houtai -p 81:80 newmynginx
577f20eff3f633452199ec74617192a75bc34543b2f7fa75e57ebbe36da56b7e
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
577f20eff3f6 newmynginx "/docker-entrypoint.…" 6 seconds ago Up 6 seconds 0.0.0.0:81->80/tcp mynxgin_houtai
8bd4c69fec01 191c4017dcdd "docker-entrypoint.s…" 21 minutes ago Up 21 minutes 0.0.0.0:6379->6379/tcp myredis
88343f5b3a3c nginx "/docker-entrypoint.…" About an hour ago Up About a minute 0.0.0.0:80->80/tcp mynginx
[root@localhost data]#
-
3)访问
image.png
- 4)镜像备份导出
docker save -o 导出的文件命名的名称.tar 需要导出镜像的名称
如:
[root@localhost data]# docker save -o houtaiquandian.tar newmynginx
[root@localhost data]# ls
backup bak ceshi houtaiquandian.tar html jiao QQpro www wwwroot wwwroot2
[root@localhost data]#
最终生成了
houtaiquandian.tar
- 5)镜像导入
前提,我们试一试把先用一些的镜像都删除
1:先把基于之前镜像所运行的容器给删除
停止容器
[root@localhost data]# docker stop mynginx
[root@localhost data]# docker stop mynxgin_houtai
然后删除容器:
[root@localhost data]# docker rm mynginx
mynginx
[root@localhost data]# docker rm mynxgin_houtai
mynxgin_houtai
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newmynginx latest cd903b9a327f 21 minutes ago 146MB
tomcat latest 891fcd9c5b3a 2 weeks ago 647MB
nginx latest f35646e83998 3 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
centos centos7.1.1503 e1430271e2f9 20 months ago 212MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost data]#
删除镜像:
[root@localhost data]# docker rmi newmynginx
Untagged: newmynginx:latest
Deleted: sha256:cd903b9a327f6e622336dd1306001d26c7d398da6eb82a045afd8b10e2f443e6
Deleted: sha256:a0328d3ea0d485f161cf1e7c38fc7f79dd6815272967e9a4d768c0cf614f4ad7
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 891fcd9c5b3a 2 weeks ago 647MB
nginx latest f35646e83998 3 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
centos centos7.1.1503 e1430271e2f9 20 months ago 212MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost data]#
查看已删除了newmynginx 的镜像
执行恢复或说导入
[root@localhost data]# docker load -i houtaiquandian.tar
2bfa788663b7: Loading layer [==================================================>] 13.35MB/13.35MB
Loaded image: newmynginx:latest
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newmynginx latest cd903b9a327f 24 minutes ago 146MB
tomcat latest 891fcd9c5b3a 2 weeks ago 647MB
nginx latest f35646e83998 3 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
centos centos7.1.1503 e1430271e2f9 20 months ago 212MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost data]#
导入成功后发现 newmynginx 的镜像又恢复了!
- 6)继续运行测试
[root@localhost data]# docker run -di --name daorunginx -p 80:80 newmynginx
cb252d0bb06e54abe65468f08a9decb0b4b3760a689c62264a7c5e0000431739
[root@localhost data]#
测试访问正常
网友评论