Docker 容器因为它的快速部署被深受喜爱。本文记录 Docker 容器的导出与导入,分别用到 Docker 的 export 和 import 命令。
1、查看正在运行的容器:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d6cc9176e469 centos:git "/bin/bash" 58 seconds ago Up 58 seconds boring_banach 52d93cf4ed12 daocloud.io/daocloud/dao-2048:master-a2c564e "/bin/sh -c 'sed -i 3 hours ago Up 3 hours 443/tcp, 0.0.0.0:8888->80/tcp lonely_meitner
2、导出容器 d6cc9176e469 (注:导出的是容器,而非镜像image) :
[root@localhost ~]# docker export d6cc9176e469 > centos_git.tar
[root@localhost ~]#
3、拷贝centos_git.tar 文件到待部署的宿主机上,导入tar文件,制作成镜像image:
[root@localhost src]# cat centos_git.tar | docker import - image_name:tag sha256:a22060ebe7a43df3c35dc41f09793efc196ebb3acf8b6ba44dca3be55d94fa9b [root@localhost src]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE image_name tag a22060ebe7a4 9 seconds ago 314MB [root@localhost src]#
4、运行导入的镜像:(按照镜像的运行方式运行),例如:以交互的方式,新开一个终端运行
docker run -it image_name:tag /bin/bash
[root@localhost local]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0beb1fb02360 image_name:tag "/bin/bash" 2 minutes ago Up 2 minutes sad_kalam
注: 运行导入镜像时,需要再镜像后边添加 command ,本例中为 “/bin/bash”,如果缺失,会出现
[root@localhost local]# docker run -it image_name:tag docker: Error response from daemon: No command specified. See 'docker run --help'.
导入镜像的command 具体是什么,需要查看远容器启动是的命令,如在源docker 中运行 docker ps , 即可查到对应的 command 。
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c55acd4f404f centos:git "/bin/bash" 3 minutes ago Up 3 minutes jolly_einstein 52d93cf4ed12 daocloud.io/daocloud/dao-2048:master-a2c564e "/bin/sh -c 'sed -i 3 hours ago Up 3 hours 443/tcp, 0.0.0.0:8888->80/tcp lonely_meitner
是不是很简单方便,赶紧来试试吧!