Docker入门-镜像运用篇(2)

列出镜像

  • 列出已下载的镜像,运用docker image ls举行检察 如下图
[root@host ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               d131e0fa2585        2 weeks ago         102MB
hello-world         latest              fce289e99eb9        4 months ago        1.84kB

列表包含了 堆栈名标签镜像 ID建立时刻 和 所占用的空间

镜像体积

  • docker image ls 列表中的镜像体积总和并非是一切镜像现实硬盘斲丧。由于 Docker 镜像是多层存储构造,而且能够继续、复用,因而差别镜像能够会由于运用雷同的基本镜像,从而具有合营的层。由于 Docker 运用 Union FS,雷同的层只需要保留一份即可,因而现实镜像硬盘占用空间极能够要比这个列表镜像大小的总和要小的多。
[root@host ~]#  docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              2                   2                   101.8MB             0B (0%)
Containers          4                   1                   0B                  0B
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B

虚悬镜像

跟着官方镜像保护,宣布了新版本后,从新 docker pull xxx 时,xxx 这个镜像名被转移到了新下载的镜像身上,而旧的镜像上的这个称号则被作废,从而成为了 <none>。除了 docker pull 能够致使这类状况,docker build 也一样能够致使这类征象。由于新旧镜像同名,旧镜像称号被作废,从而涌现堆栈名、标签均为 <none> 的镜像。这类无标签镜像也被称为 虚悬镜像(dangling image) ,能够用下面的敕令特地显现这类镜像:

  • docker image ls -f dangling=true 显现虚悬镜像
  • docker image prune 删除虚悬镜像

中间层镜像

  • docker image ls -a docker image ls只能显现顶层的镜像 显现一切的是后边加参数-a

排列部份镜像

  • docker image ls ubuntu => 依据堆栈名ubuntu列出镜像
  • docker image ls ubuntu:18.04 指定堆栈名和标签
  • docker image ls -f since=mongo:3.2 -f filter 过滤器来挑选查找

特定花样来显现

docker image ls会输出一个完成的表格,然则有时刻没必要显现这么多,能够只需要镜像的ID就够用了,这时刻运用 -p 参数

[root@host ~]# docker image ls -q
d131e0fa2585
fce289e99eb9

--filter 合营 -q 产生出指定局限的 ID 列表,然后送给另一个 docker 敕令作为参数,从而针对这组实体成批的举行某种操纵的做法在 Docker 敕令行运用过程中异常罕见,不仅仅是镜像,未来我们会在各个敕令中看到这类搭配以完成很壮大的功用。因而每次在文档看到过滤器后,能够多注重一下它们的用法。

别的一些时刻,我们能够只是对表格的构造不满意,愿望本身构造列;或许不愿望有题目,如许轻易别的顺序剖析效果等,这时刻就需要GO模板语法

[root@host ~]# docker image ls --format "{{.ID}}: {{.Repository}}"
d131e0fa2585: ubuntu
fce289e99eb9: hello-world

表格 展现都能够自定义的经由过程模板{{}}来处置惩罚 相似vue中的模板语法

[root@host ~]# docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID            REPOSITORY          TAG
d131e0fa2585        ubuntu              18.04
fce289e99eb9        hello-world         latest

删除镜像

假如要删除当地镜像能够运用docker image rm敕令:

docker image rm [选项] <镜像> [<镜像2> ...]

用镜像名、ID、择要来删除镜像

<镜像>能够是 镜像的短 ID、镜像长ID镜像择要或许镜像名字

[root@host ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               d131e0fa2585        2 weeks ago         102MB


docker image rm d13 

能够用镜像的完全 ID,来删除镜像。更多的时刻是用 短 ID 来删除镜像。docker image ls 默许列出的就已是短 ID 了,平常取前3个字符以上,只需能够辨别于别的镜像便可。

固然,更准确的是运用 镜像择要 删除镜像。
docker image ls --digests检察择要
docker image rm [择要的值]

运用 docker image ls 敕令来合营

能够运用 docker image ls -q 来合营运用 docker image rm,如许能够成批的删除愿望删除的镜像。我们在“镜像列表”章节引见过许多过滤镜像列表的体式格局都能够拿过来运用。

删除一切在 mongo:3.4 之前的镜像:

docker image rm $(docker image ls -q -f before=mongo:3.4)

删除一切堆栈名为 redis 的镜像

docker image rm $(docker image ls -q redis)
    原文作者:师宁丶
    原文地址: https://segmentfault.com/a/1190000019165979
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞