# docker images REPOSITORY TAG IMAGE ID CREATED SIZE bash latest 5557e073f11c 2 weeks ago 13MB nginx latest 605c77e624dd 3 weeks ago 141MB centos latest 5d0da3dc9764 4 months ago 231MB
2.1.2 使用docker image命令查看
# docker image list REPOSITORY TAG IMAGE ID CREATED SIZE bash latest 5557e073f11c 2 weeks ago 13MB nginx latest 605c77e624dd 3 weeks ago 141MB centos latest 5d0da3dc9764 4 months ago 231MB
输出 NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 6987 [OK] ansible/centos7-ansible Ansible on Centos7 135 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 135 [OK] jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 121 [OK]
2.2.2 Docker Hub Web界面搜索
2.3 Docker 容器镜像下载
# docker pull centos
2.4 Docker容器镜像删除方法
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE bash latest 5557e073f11c 2 weeks ago 13MB nginx latest 605c77e624dd 3 weeks ago 141MB centos latest 5d0da3dc9764 4 months ago 231MB
# ls /var/lib/docker/overlay2/85c4c5ecdac6c0d197f899dac227b9d493911a9a5820eac501bb5e9ae361f4c7 committed diff link # ls /var/lib/docker/overlay2/85c4c5ecdac6c0d197f899dac227b9d493911a9a5820eac501bb5e9ae361f4c7/diff bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr
上示例中,镜像元数据和镜像层内容是分开存储的。因此通过 cache-id 我们需要到 /var/lib/docker/overlay2 目录下查看镜像层内容,它就存在 diff 目录下,其中 link 存储的是镜像层对应的短 ID,后面会看到它的用场。
镜像层文件内容 # ls /var/lib/docker/overlay2/57e1f1b11e26f748161b7fccbf2ba6b24c2f98dc8a821729f0be215ad267498c committed diff link lower work # ls /var/lib/docker/overlay2/57e1f1b11e26f748161b7fccbf2ba6b24c2f98dc8a821729f0be215ad267498c/diff/ docker-entrypoint.d etc lib tmp usr var
镜像层文件内容短 ID # cat /var/lib/docker/overlay2/57e1f1b11e26f748161b7fccbf2ba6b24c2f98dc8a821729f0be215ad267498c/link 24GM2IZVPTUROAG7AWJO5ZWE6B
“父”镜像层文件内容短 ID # cat /var/lib/docker/overlay2/57e1f1b11e26f748161b7fccbf2ba6b24c2f98dc8a821729f0be215ad267498c/lower l/SICZO4QNVZEVOIJ4HDXVDKNYA2
找到最底层文件内容和“中间层”文件内容,再去找最顶层文件内容就变的不难了
2.4 Docker容器与镜像
通过 docker run 命令启动一个镜像为 nginx的容器:
# docker run -d nginx:latest 3272831107a3499afe8160b0cd423e2ac4223522f1995b7be3504a1d3d272878 # docker ps | grep nginx 3272831107a3 nginx:latest "/docker-entrypoint.…"11 seconds ago Up 9 seconds 80/tcp angry_beaver
# mount | grep overlay overlay on /var/lib/docker/overlay2/b3f5c8b42ac055c715216e376cfe44571f618a876f481533ec1434aa0bc4f8ed/merged type overlay (rw,relatime,seclabel,lowerdir=/var/lib/docker/overlay2/l/MS2X66BYF6UZ7EKUWMZJKCF4HO:/var/lib/docker/overlay2/l/ODJROQUGY3WQMOGQ3BLYZGIAG4:/var/lib/docker/overlay2/l/Q5LOBFJRH5M7M5CMSWW5L4VYOY:/var/lib/docker/overlay2/l/ZR35FN2E3WEARZV4HLRU373FT7:/var/lib/docker/overlay2/l/NSM2PTAT6TIT2H6G3HFNGZJH5N:/var/lib/docker/overlay2/l/24GM2IZVPTUROAG7AWJO5ZWE6B:/var/lib/docker/overlay2/l/SICZO4QNVZEVOIJ4HDXVDKNYA2,upperdir=/var/lib/docker/overlay2/b3f5c8b42ac055c715216e376cfe44571f618a876f481533ec1434aa0bc4f8ed/diff,workdir=/var/lib/docker/overla 2/b3f5c8b42ac055c715216e376cfe44571f618a876f481533ec1434aa0bc4f8ed/work)
可以看到,启动容器会 mount 一个 overlay 的联合文件系统到容器内。这个文件系统由三层组成:
启动了容器,docker 将镜像的内容 mount 到容器中。那么,如果在容器内写文件会对镜像有什么影响呢?
2.5 容器内写文件
不难理解,镜像层是只读的,在容器中写文件其实是将文件写入到 overlay 的可读写层。
这里有几个 case 可以测试:
读写层不存在该文件,只读层存在。
读写层存在该文件,只读层不存在。
读写层和只读层都不存在该文件。
我们简单构建一种读写层和只读层都不存在的场景:
# docker run -it centos:latest bash [root@355e99982248 /]# touch msb.txt [root@355e99982248 /]# ls bin etc lib lost+found mnt opt root sbin sys usr dev home lib64 media msb.txt proc run srv tmp var
查看读写层是否有该文件:
查看镜像是否有变化 # docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest d13c942271d6 2 weeks ago 72.8MB bash latest 5557e073f11c 2 weeks ago 13MB nginx latest 605c77e624dd 3 weeks ago 141MB centos latest 5d0da3dc9764 4 months ago 231MB
[root@localhost ~]# ls /var/lib/docker/overlay2/b17bc5c5103514923a30983c48f909e06f366b7aa1e85f112b67abb3ef5cd0cb committed diff link [root@localhost ~]# ls /var/lib/docker/overlay2/b17bc5c5103514923a30983c48f909e06f366b7aa1e85f112b67abb3ef5cd0cb/diff/ bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr
查看容器是否有变化 [root@localhost ~]# mount | grep overlay type overlay (rw,relatime,seclabel,lowerdir=/var/lib/docker/overlay2/l/R2W2LEMDPRIUFYDVSLIQSCYTGX:/var/lib/docker/overlay2/l/R2XTGODAA67NQJM44MIKMDUF4W,upperdir=/var/lib/docker overlay2/7f0b54c748171872ce564305e394547555cb1182abf802c2262384be3dc78a8f/diff,workdir=/var/lib/docker/overlay2/7f0b54c748171872ce564305e394547555cb1182abf802c2262384be3dc78a8f/work)
[root@localhost ~]# ls -l /var/lib/docker/overlay2/l/ 总用量 0
[root@localhost ~]# ls /var/lib/docker/overlay2/7f0b54c748171872ce564305e394547555cb1182abf802c2262384be3dc78a8f/diff msb.txt
[root@localhost ~]# ls /var/lib/docker/overlay2/7f0b54c748171872ce564305e394547555cb1182abf802c2262384be3dc78a8f/merged/ bin etc lib lost+found mnt opt root sbin sys usr dev home lib64 media msb.txt proc run srv tmp var
# cd /var/lib/docker/image/overlay2/layerdb/sha256/92f7208b1cc0b5cc8fe214a4b0178aa4962b58af8ec535ee7211f335b1e0ed3b [root@19292f7208b1cc0b5cc8fe214a4b0178aa4962b58af8ec535ee7211f335b1e0ed3b]# ls cache-iddiff parent size tar-split.json.gz
[root@19292f7208b1cc0b5cc8fe214a4b0178aa4962b58af8ec535ee7211f335b1e0ed3b]# ls /var/lib/docker/overlay2/250dc0b4f2c5f27952241a55cd4c286bfaaf8af4b77c9d0a38976df4c147cb95 diff link lower work
[root@19292f7208b1cc0b5cc8fe214a4b0178aa4962b58af8ec535ee7211f335b1e0ed3b]# ls /var/lib/docker/overlay2/250dc0b4f2c5f27952241a55cd4c286bfaaf8af4b77c9d0a38976df4c147cb95/diff msb.txt
3.2 docker save
导出容器镜像,方便分享。
# docker save -o centos.tar centos:latest
# ls
centos.tar
3.3 docker load
把他人分享的容器镜像导入到本地,这通常是容器镜像分发方式之一。
# docker load -i centos.tar
3.4 docker export
把正在运行的容器导出
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 355e99982248 centos:latest "bash"7 hours ago Up 7 hours fervent_perlman
# docker export -o centos7.tar 355e99982248
# ls centos7.tar
3.5 docker import
导入使用docker export导入的容器做为本地容器镜像。
# ls centos7.tar
# docker import centos7.tar centos7:v1
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 v1 3639f9a13231 17 seconds ago 231MB