本文讨论一下与docker镜像相关的几个接口.
制作镜像相关 :
使用dockerfile制作一个sshd镜像
从container制作镜像(使用docker commit)
制作base镜像
另外Docker还可以导出container到一个文件(docker export)
将文件导入成为镜像
(docker import)
.
同时还支持导出已有镜像到一个文件,
(docker save)
将镜像导入
(docker load)
.
使用举例 :
save镜像, load to image :
export container, import to image :
[参考]
查看镜像
[root@db-172-16-3-221 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry 0.8.1 3e7767ddd728 5 weeks ago 427.9 MB
导出到一个tar文件
[root@db-172-16-3-221 ~]# docker save -o ./registry.tar 3e7767ddd728
[root@db-172-16-3-221 ~]# ll -h registry.tar
-rw-r--r-- 1 root root 427M Dec 5 14:09 registry.tar
将tar文件拷贝到另一台主机
[root@db-172-16-3-221 ~]# scp registry.tar 172.16.3.150:/opt/soft_bak/
在另一台主机导入
[root@localhost soft_bak]# docker load -i /opt/soft_bak/registry.tar
导入后未命名
[root@localhost soft_bak]# docker images
<none> <none> 3e7767ddd728 5 weeks ago 427.9 MB
打上tag
[root@localhost soft_bak]# docker tag 3e7767ddd728 digoal/registry
[root@localhost soft_bak]# docker images
digoal/registry latest 3e7767ddd728 5 weeks ago 427.9 MB
参考以下方法使用刚才load进来的image创建一个私有registry.
http://blog.163.com/digoal@126/blog/static/163877040201410393931894/
[root@localhost registry_conf]# docker run -d --net="host" -p 5000:5000 -v /data01/registry_conf:/registory_conf -v /data01/registry_sto:/registry_sto -e DOCKER_REGISTRY_CONFIG=/registory_conf/config_sample.yml -e STORAGE_PATH=/registry_sto -e SETTINGS_FLAVOR=local --name=registry digoal/registry
ea75c68e44b68d186b1551a6ddc8b70b1b0a39179ca28166eacd1be5751f7b27
[root@localhost registry_conf]#
[root@localhost registry_conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea75c68e44b6 digoal/registry:latest "/bin/sh -c 'exec do 3 seconds ago Up 2 seconds registry
export container, import to image :
查看当前正在运行或已经停止的container
[root@localhost registry_conf]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea75c68e44b6 digoal/registry:latest "/bin/sh -c 'exec do 4 minutes ago Up 4 minutes registry
4c098166cc5d digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago osd4
f6df72b9780f digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago osd3
02d922ce295b digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago osd2
59c41fc9560e digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago deploy
34a7ceefcfd3 digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago osd1
9c9391eb3e1a digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago mon3
5de0fae0d8bd digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago mon2
fa24ec5115f6 digoal/sshd:latest "/usr/sbin/sshd -D" 7 days ago Exited (0) 28 hours ago mon1
使用docker export将container到处到tar文件
[root@localhost registry_conf]# docker export deploy > ./sshd.tar
[root@localhost registry_conf]# ll -h sshd.tar
-rw-r--r-- 1 root root 606M Dec 5 22:20 sshd.tar
使用docker import导入
[root@localhost registry_conf]# cat sshd.tar | docker import - digoal/sshd_deploy
9110f1eb1ce1e95285e9b0daa7451e7de5165e350df8289aa92a36cacf499bcc
查看导入的镜像
[root@localhost registry_conf]# docker images digoal/*
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
digoal/sshd_deploy latest 9110f1eb1ce1 19 seconds ago 620.4 MB
digoal/sshd latest 3e5d8edfaeee 8 days ago 298.1 MB
digoal/registry latest 3e7767ddd728 5 weeks ago 427.9 MB
使用导入的镜像, 创建一个容器
[root@localhost registry_conf]# docker run -t -i --rm digoal/sshd_deploy /bin/bash
容器中安装的软件仍在, 所以在容器中的改变都被打包到镜像了, 和docker commit制作的镜像类似.
[root@1a3adda5d7b9 /]# rpm -qa|grep deploy
ceph-deploy-1.5.20-0.noarch
ph-deploy
[root@1a3adda5d7b9 /]# cd /var/lib/ceph/
[root@1a3adda5d7b9 ceph]# ll
total 24
drwxr-xr-x 2 root root 4096 Oct 29 18:35 bootstrap-mds
drwxr-xr-x 2 root root 4096 Oct 29 18:35 bootstrap-osd
drwxr-xr-x 2 root root 4096 Oct 29 18:35 mds
drwxr-xr-x 2 root root 4096 Oct 29 18:35 mon
drwxr-xr-x 2 root root 4096 Oct 29 18:35 osd
drwxr-xr-x 2 root root 4096 Oct 29 18:35 tmp
最后docker镜像查看还支持tree输出. 可以列出镜像之间的关系.
例如digoal/postgresql:9.3.5是使用centos:centos6镜像制作的, 所以打印出了继承关系
还有很多未命名的, 是制作镜像时使用RUN产生的中间镜像.
[root@localhost registry_conf]# docker images -a --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
└─5b12ef8fd570 Virtual Size: 0 B
├─192178b11d36 Virtual Size: 466.9 MB Tags: centos:centos5
├─70441cac1ed5 Virtual Size: 215.8 MB Tags: centos:centos6
│ └─d70b9ae9e9ff Virtual Size: 330.3 MB Tags: 172.16.3.221:5000/digoal/postgresql:9.3.5
└─ae0c2d0bdc10 Virtual Size: 224 MB Tags: centos:centos7
└─3c5e418bb4b1 Virtual Size: 224 MB
└─c48a513d5431 Virtual Size: 298.1 MB
└─e61b7a8bb4d9 Virtual Size: 298.1 MB
└─8ed4d6eb45c1 Virtual Size: 298.1 MB
└─ecb67638a0df Virtual Size: 298.1 MB
└─2b73dfeaebf1 Virtual Size: 298.1 MB
└─6e0fcfed929b Virtual Size: 298.1 MB
└─3e5d8edfaeee Virtual Size: 298.1 MB Tags: digoal/sshd:latest
[参考]