[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 7ee26c09afb3 3 days ago 462MB ubuntu 16.04 7e87e2b3bf7a 4 days ago 117MB hello-world latest fce289e99eb9 3 weeks ago 1.84kB
另外一个需要注意的问题是,docker image ls 列表中的镜像体积总和并非是所有镜像实际硬盘消耗。由于 Docker 镜像是多层存储结构,并且可以继承、复用,因此不同镜像可能会因为使用相同的基础镜像,从而拥有共同的层。由于 Docker 使用 Union FS,相同的层只需要保存一份即可,因此实际镜像硬盘占用空间很可能要比这个列表镜像大小的总和要小的多
中间层镜像
为了节省空间,Docker会复用中间层镜像,而docker image ls只是显示了顶层镜像
如果希望显示包括中间层镜像在内的所有镜像的话,需要加 -a 参数。
1
docker image ls -a
部分镜像查看
docker image ls会列出所有的顶层镜像,如果只想查看特定镜像,可以使用
1 2 3
$ docker image ls tomcat REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 7ee26c09afb3 3 days ago 462MB
还可以同时指定仓库和标签
1 2 3
$ docker image ls ubuntu:16.04 REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 16.04 7e87e2b3bf7a 4 days ago 117MB
Docker删除镜像
如果要删除本地的镜像,可以使用 docker image rm 命令,其格式为:
1
$ docker image rm 镜像1 [镜像2]
其中,<镜像> 可以是 镜像短 ID、镜像长 ID、镜像名 或者 镜像摘要。
我们可以用镜像的完整 ID,也称为 长 ID,来删除镜像。使用脚本的时候可能会用长 ID,但是人工输入就太累了,所以更多的时候是用 短 ID 来删除镜像。docker image ls 默认列出的就已经是短 ID 了,一般取前3个字符以上,只要足够区分于别的镜像就可以了
root@3b9702b9719e:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx root@3b9702b9719e:/# cd /usr/share/nginx root@3b9702b9719e:/usr/share/nginx# ls html root@3b9702b9719e:/usr/share/nginx# echo '<h1>hello,ranger,this is nginx contianer on docker<h1>' > html bash: html: Is a directory root@3b9702b9719e:/usr/share/nginx# echo '<h1>hello,ranger,this is nginx contianer on docker<h1>' > html/index.html root@3b9702b9719e:/usr/share/nginx#
重新访问就可以看到改变后的页面
改变了容器中的文件,可以查看这些改动
1 2 3 4 5 6 7 8 9
root@3b9702b9719e:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx root@3b9702b9719e:/# cd /usr/share/nginx root@3b9702b9719e:/usr/share/nginx# ls html root@3b9702b9719e:/usr/share/nginx# echo '<h1>hello,ranger,this is nginx contianer on docker<h1>' > html bash: html: Is a directory root@3b9702b9719e:/usr/share/nginx# echo '<h1>hello,ranger,this is nginx contianer on docker<h1>' > html/index.html root@3b9702b9719e:/usr/share/nginx#
加入我们现在想把这些改动保存成一个新的镜像,以后使用,就可以使用docker commit
docker commit 的语法格式为:
1
docker commit [选项] <容器ID或容器名> [<仓库名>[:<标签>]]
现在查看有哪些镜像
1 2 3 4 5 6 7
[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx v2 ac471dbf7f1f About a minute ago 109MB tomcat latest 7ee26c09afb3 3 days ago 462MB nginx latest 42b4762643dc 4 days ago 109MB ubuntu 16.04 7e87e2b3bf7a 4 days ago 117MB hello-world latest fce289e99eb9 3 weeks ago 1.84kB
[root@localhost ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b9702b9719e nginx "nginx -g 'daemon of…" 38 minutes ago Up 29 minutes 0.0.0.0:80->80/tcp webserver 32e475d34375 tomcat "catalina.sh run" About an hour ago Up 7 seconds 8080/tcp compassionate_bardeen [root@localhost ~]#
[root@localhost ~]# docker run -dit ubuntu 4afc7ff447ac61e14242df8c2f8a26d8c54b8bee822dbfa2cd5340084b7e8e74 [root@localhost ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4afc7ff447ac ubuntu "/bin/bash" 21 seconds ago Up 15 seconds blissful_wiles 3b9702b9719e nginx "nginx -g 'daemon of…" About an hour ago Up 37 minutes 0.0.0.0:80->80/tcp webserver 32e475d34375 tomcat "catalina.sh run" About an hour ago Up 3 minutes 8080/tcp compassionate_bardeen [root@localhost ~]# docker exec -i 4a bash ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[root@localhost ~]# docker exec -i 4a bash ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys
[root@localhost ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b9702b9719e nginx "nginx -g 'daemon of…" About an hour ago Up 45 minutes 0.0.0.0:80->80/tcp webserver 32e475d34375 tomcat "catalina.sh run" 2 hours ago Up 11 minutes 8080/tcp compassionate_bardeen [root@localhost ~]# docker export 3b nginx.tar "docker export" requires exactly 1 argument. See 'docker export --help'.
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive [root@localhost ~]# docker export 3b > nginx.tar [root@localhost ~]# ls anaconda-ks.cfg Git检出代码工作脚本 nginx.tar original-ks.cfg
[root@localhost ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c507aac48152 ubuntu "/bin/bash" 35 minutes ago Exited (0) 35 minutes ago flamboyant_lovelace 77a1908342b8 ubuntu "bash" 38 minutes ago Exited (127) 37 minutes ago ecstatic_pike 3b9702b9719e nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp webserver 3c1ee95ddbfd hello-world "/hello" 2 hours ago Exited (0) 2 hours ago confident_borg 32e475d34375 tomcat "catalina.sh run" 2 hours ago Exited (143) About a minute ago compassionate_bardeen 0955cc000279 tomcat "-it" 2 hours ago Created 8080/tcp cocky_tesla cd725259b2b3 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago reverent_knuth [root@localhost ~]# docker container rm c5 c5 [root@localhost ~]# docker container rm 77a 3c 32e 09 cd 77a 3c 32e 09 cd [root@localhost ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b9702b9719e nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp webserver [root@localhost ~]#
清楚所有终止的容器
用 docker container ls -a 命令可以查看所有已经创建的包括终止状态的容器,如果数量太多要一个个删除可能会很麻烦,用下面的命令可以清理掉所有处于终止状态的容器。