docker深入1-导入导出images和container的方式

docker深入1-导入导出images和container的方式



环境准备:
[Jack@test101 base]$ docker run -d --name app1 -p 10023:22 pcnk/base:v2
[Jack@test101 base]$ docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS              PORTS                                       NAMES
c10446ab9620        pcnk/base:v2          "/usr/bin/supervisor   9 seconds ago       Up 7 seconds        0.0.0.0:10023->22/tcp                       app1                


[Jack@test101 base]$ docker commit -m "ssh,supervisor" -a "pcnk" c10446ab9620 base:latest    
29e0b4550969220103e7179df62f31f3f1345a364c4fb103f73a5ba3b6f8fc5d


[Jack@test101 base]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
base                latest              29e0b4550969        About a minute ago   251.3 MB



一、container的导入导出
保存:
[Jack@test101 base]$ docker export app1 >/home/datacenter/docker/containers/app1.tar

删除:
[Jack@test101 base]$ docker stop app1
app1
[Jack@test101 base]$ docker rm app1  
app1


恢复:
[Jack@test101 base]$ cat /home/datacenter/docker/containers/app1.tar |docker import - base_import
c9761ff46cf1ff3e0f1de04ed2fcf52b0b591b6fd0ea943611f2066046a17aae
[Jack@test101 base]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
base_import         latest              c9761ff46cf1        15 seconds ago      228.1 MB
base                latest              29e0b4550969        13 minutes ago      251.3 MB
website/sync        latest              1bda5d872bf7        About an hour ago   508 MB
pcnk/base           v2                  064aa35dfcef        7 hours ago         251.3 MB
centos              centos7             fd44297e2ddb        2 weeks ago         215.7 MB
centos              latest              fd44297e2ddb        2 weeks ago         215.7 MB
centos              7                   fd44297e2ddb        2 weeks ago         215.7 MB
[Jack@test101 base]$ docker images -tree
Warning: '-tree' is deprecated, it will be removed soon. See usage.
├─c9761ff46cf1 Virtual Size: 228.1 MB Tags: base_import:latest
└─6941bfcbbfca Virtual Size: 0 B
  └─41459f052977 Virtual Size: 215.7 MB
    └─fd44297e2ddb Virtual Size: 215.7 MB Tags: centos:7, centos:centos7, centos:latest
      └─751a85a0d00b Virtual Size: 215.7 MB
        └─19b009f08542 Virtual Size: 228.1 MB
          └─420cb9e73c70 Virtual Size: 251.3 MB
            └─e97f94a79de0 Virtual Size: 251.3 MB
              └─fde06c761bde Virtual Size: 251.3 MB
                └─dbfae1099999 Virtual Size: 251.3 MB
                  └─064aa35dfcef Virtual Size: 251.3 MB Tags: pcnk/base:v2
                    └─b41b76b51e1a Virtual Size: 505.9 MB
                      └─1bda5d872bf7 Virtual Size: 508 MB Tags: website/sync:latest

没有之前的层的信息,变成了独立的一层:
├─c9761ff46cf1 Virtual Size: 228.1 MB Tags: base_import:latest

二、images的导入导出
注:据文档解释,这种方式,不会丢失提交历史和层,可以回滚,但,目前尚未操作过回滚。

保存:
[Jack@test101 base]$ docker save base > /home/datacenter/docker/images/base.tar 

删除:
[Jack@test101 base]$ docker rmi 29e0b4550969
Untagged: base:latest
Deleted: 29e0b4550969220103e7179df62f31f3f1345a364c4fb103f73a5ba3b6f8fc5d

恢复:
[Jack@test101 base]$ docker load </home/datacenter/docker/images/base.tar           
[Jack@test101 base]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
base_import         latest              c9761ff46cf1        About a minute ago   228.1 MB
base                latest              29e0b4550969        17 minutes ago       251.3 MB
website/sync        latest              1bda5d872bf7        About an hour ago    508 MB
pcnk/base           v2                  064aa35dfcef        7 hours ago          251.3 MB
centos              7                   fd44297e2ddb        2 weeks ago          215.7 MB
centos              centos7             fd44297e2ddb        2 weeks ago          215.7 MB
centos              latest              fd44297e2ddb        2 weeks ago          215.7 MB
[Jack@test101 base]$ docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─c9761ff46cf1 Virtual Size: 228.1 MB Tags: base_import:latest
└─6941bfcbbfca Virtual Size: 0 B
  └─41459f052977 Virtual Size: 215.7 MB
    └─fd44297e2ddb Virtual Size: 215.7 MB Tags: centos:latest, centos:7, centos:centos7
      └─751a85a0d00b Virtual Size: 215.7 MB
        └─19b009f08542 Virtual Size: 228.1 MB
          └─420cb9e73c70 Virtual Size: 251.3 MB
            └─e97f94a79de0 Virtual Size: 251.3 MB
              └─fde06c761bde Virtual Size: 251.3 MB
                └─dbfae1099999 Virtual Size: 251.3 MB
                  └─064aa35dfcef Virtual Size: 251.3 MB Tags: pcnk/base:v2
                    ├─29e0b4550969 Virtual Size: 251.3 MB Tags: base:latest
                    └─b41b76b51e1a Virtual Size: 505.9 MB
                      └─1bda5d872bf7 Virtual Size: 508 MB Tags: website/sync:latest

恢复出来的image,还是有原来的层的信息:
                    ├─29e0b4550969 Virtual Size: 251.3 MB Tags: base:latest

三、查看history可以观察到这个image的build过程

[Jack@test101 base]$ docker history base_import
IMAGE               CREATED             CREATED BY          SIZE
c9761ff46cf1        41 minutes ago                          228.1 MB
[Jack@test101 base]$ docker history base
IMAGE               CREATED             CREATED BY                                      SIZE
29e0b4550969        57 minutes ago                                                      699 B
064aa35dfcef        8 hours ago         /bin/sh -c #(nop) ENTRYPOINT [/usr/bin/superv   0 B
dbfae1099999        8 hours ago         /bin/sh -c #(nop) COPY file:b96e75ea47b2d1c16   41 B
fde06c761bde        8 hours ago         /bin/sh -c set -x         && /bin/sed -i 's/.   12.99 kB
e97f94a79de0        8 hours ago         /bin/sh -c #(nop) ADD file:0cce39ed9dc5d31da4   807 B
420cb9e73c70        8 hours ago         /bin/sh -c rpm -ivh http://dl.fedoraproject.o   23.25 MB
19b009f08542        8 hours ago         /bin/sh -c yum -y update; yum clean all         12.41 MB
751a85a0d00b        8 hours ago         /bin/sh -c #(nop) MAINTAINER pcnk               0 B
fd44297e2ddb        2 weeks ago         /bin/sh -c #(nop) CMD ["/bin/bash"]             0 B
41459f052977        2 weeks ago         /bin/sh -c #(nop) ADD file:be2a22bb15fbbbf24b   215.7 MB
6941bfcbbfca        2 weeks ago         /bin/sh -c #(nop) MAINTAINER The CentOS Proje   0 B


[Jack@test101 base]$ docker history --help

Usage: docker history [OPTIONS] IMAGE

Show the history of an image

  --help=false         Print usage
  --no-trunc=false     Don't truncate output
  -q, --quiet=false    Only show numeric IDs
  
[Jack@test101 base]$ docker history --no-trunc=true base
IMAGE                                                              CREATED             CREATED BY                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               SIZE
29e0b4550969220103e7179df62f31f3f1345a364c4fb103f73a5ba3b6f8fc5d   58 minutes ago                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               699 B
064aa35dfcef778713ad1b4cc02add4e8e48425741768595d7b5909961f74cee   8 hours ago         /bin/sh -c #(nop) ENTRYPOINT [/usr/bin/supervisord]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0 B
dbfae1099999316cc130ac467768e6c957a98682076b659642d0d8c5ec7587e2   8 hours ago         /bin/sh -c #(nop) COPY file:b96e75ea47b2d1c1601aecc355cf21eb6f80d043f512fcedec81a4018d2776a1 in /etc/supervisor.d/sshd.ini                                                                                                                                                                                                                                                                                                                                                                                                                                                                               41 B
fde06c761bde092a37890e94c8d68e16ef0ced5f1c70357a2cc3c4aab51cbe3d   8 hours ago         /bin/sh -c set -x         && /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd         && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''         && mkdir /var/run/sshd  /etc/supervisor.d         && /usr/bin/echo_supervisord_conf >/etc/supervisord.conf          && sed -i 's/nodaemon=false/nodaemon=true/' /etc/supervisord.conf         && echo -e '[include]\nfiles=/etc/supervisor.d/*.ini' >>/etc/supervisord.conf         && grep ^[^\;] /etc/supervisord.conf         && chmod 755 /root/start.sh         && ./root/start.sh   12.99 kB
e97f94a79de0fa89a437ace2a2d14d3cfbba83f8d2f0528660e9028832735d6e   8 hours ago         /bin/sh -c #(nop) ADD file:0cce39ed9dc5d31da4c1112794629d4bab2f82e18e7a5bcabdf6defeae4e0657 in /root/start.sh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            807 B
420cb9e73c70c2e7d20af40d66cae68ed32362072a9aae53e1855c700dc79718   8 hours ago         /bin/sh -c rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm       && yum -y install openssh-server passwd python-pip       && yum clean all       && /usr/bin/pip install supervisor                                                                                                                                                                                                                                                                                                                                                                                 23.25 MB
19b009f08542bcd1e8a5e17e4b1bd60e3e1cadbafcf4eaae914a3ba67784b73e   8 hours ago         /bin/sh -c yum -y update; yum clean all                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  12.41 MB
751a85a0d00bcbc6fac2051f79a978c9091ed03b6fa451ab4bfff53d28c1f40d   8 hours ago         /bin/sh -c #(nop) MAINTAINER pcnk                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        0 B
fd44297e2ddb050ec4fa9752b7a4e3a8439061991886e2091e7c1f007c906d75   2 weeks ago         /bin/sh -c #(nop) CMD ["/bin/bash"]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0 B
41459f052977938b824dd011e1f2bec2cb4d133dfc7e1aa0e90f7c5d337ca9c4   2 weeks ago         /bin/sh -c #(nop) ADD file:be2a22bb15fbbbf24b5f13830d9ec31fad7e8bdf251906c79b05ad52cb4eef61 in /                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         215.7 MB
6941bfcbbfca7f4f48becd38f2639157042b5cf9ab8c080f1d8b6d047380ecfc   2 weeks ago         /bin/sh -c #(nop) MAINTAINER The CentOS Project <[email protected]>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   0 B


你可能感兴趣的:(docker,container,images)