ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 列出镜像 要想列出已经下载下来的镜像,可以使用 `docker image ls` 命令。 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">REPOSITORY TAG IMAGE ID CREATED SIZE</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">redis latest </span><span class="lit">5f515359c7f8</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">183</span><span class="pln"> MB</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">nginx latest </span><span class="lit">05a60462f8ba</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">181</span><span class="pln"> MB</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">mongo </span><span class="lit">3.2</span><span class="pln"> fe9198c04d62 </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">342</span><span class="pln"> MB</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pun"><</span><span class="pln">none</span><span class="pun">></span><span class="pln"> </span><span class="pun"><</span><span class="pln">none</span><span class="pun">></span><span class="pln"> </span><span class="lit">00285df0df87</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">342</span><span class="pln"> MB</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">ubuntu </span><span class="lit">16.04</span><span class="pln"> f753707788c5 </span><span class="lit">4</span><span class="pln"> weeks ago </span><span class="lit">127</span><span class="pln"> MB</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">ubuntu latest f753707788c5 </span><span class="lit">4</span><span class="pln"> weeks ago </span><span class="lit">127</span><span class="pln"> MB</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">ubuntu </span><span class="lit">14.04</span><span class="pln"> </span><span class="lit">1e0c3dd64ccd</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> weeks ago </span><span class="lit">188</span><span class="pln"> MB</span></code></li> </ol> ``` 列表包含了 `仓库名`、`标签`、`镜像 ID`、`创建时间` 以及 `所占用的空间`。 其中仓库名、标签在之前的基础概念章节已经介绍过了。**镜像 ID** 则是镜像的唯一标识,一个镜像可以对应多个**标签**。因此,在上面的例子中,我们可以看到 `ubuntu:16.04` 和 `ubuntu:latest` 拥有相同的 ID,因为它们对应的是同一个镜像。 ### 镜像体积 如果仔细观察,会注意到,这里标识的所占用空间和在 Docker Hub 上看到的镜像大小不同。比如,`ubuntu:16.04` 镜像大小,在这里是 `127 MB`,但是在 [Docker Hub](https://hub.docker.com/r/library/ubuntu/tags/) 显示的却是 `50 MB`。这是因为 Docker Hub 中显示的体积是压缩后的体积。在镜像下载和上传过程中镜像是保持着压缩状态的,因此 Docker Hub 所显示的大小是网络传输中更关心的流量大小。而 `docker image ls` 显示的是镜像下载到本地后,展开的大小,准确说,是展开后的各层所占空间的总和,因为镜像到本地后,查看空间的时候,更关心的是本地磁盘空间占用的大小。 另外一个需要注意的问题是,`docker image ls` 列表中的镜像体积总和并非是所有镜像实际硬盘消耗。由于 Docker 镜像是多层存储结构,并且可以继承、复用,因此不同镜像可能会因为使用相同的基础镜像,从而拥有共同的层。由于 Docker 使用 Union FS,相同的层只需要保存一份即可,因此实际镜像硬盘占用空间很可能要比这个列表镜像大小的总和要小的多。 你可以通过以下命令来便捷的查看镜像、容器、数据卷所占用的空间。 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker system df</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">TYPE TOTAL ACTIVE SIZE RECLAIMABLE</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="typ">Images</span><span class="pln"> </span><span class="lit">24</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">1.992GB</span><span class="pln"> </span><span class="lit">1.992GB</span><span class="pln"> </span><span class="pun">(</span><span class="lit">100</span><span class="pun">%)</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="typ">Containers</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">62.82MB</span><span class="pln"> </span><span class="lit">62.82MB</span><span class="pln"> </span><span class="pun">(</span><span class="lit">100</span><span class="pun">%)</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="typ">Local</span><span class="pln"> </span><span class="typ">Volumes</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">652.2MB</span><span class="pln"> </span><span class="lit">652.2MB</span><span class="pln"> </span><span class="pun">(</span><span class="lit">100</span><span class="pun">%)</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="typ">Build</span><span class="pln"> </span><span class="typ">Cache</span><span class="pln"> </span><span class="lit">0B</span><span class="pln"> </span><span class="lit">0B</span></code></li> </ol> ``` ### 虚悬镜像 上面的镜像列表中,还可以看到一个特殊的镜像,这个镜像既没有仓库名,也没有标签,均为 `<none>`。: ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pun"><</span><span class="pln">none</span><span class="pun">></span><span class="pln"> </span><span class="pun"><</span><span class="pln">none</span><span class="pun">></span><span class="pln"> </span><span class="lit">00285df0df87</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">342</span><span class="pln"> MB</span></code></li></ol> ``` 这个镜像原本是有镜像名和标签的,原来为 `mongo:3.2`,随着官方镜像维护,发布了新版本后,重新 `docker pull mongo:3.2` 时,`mongo:3.2` 这个镜像名被转移到了新下载的镜像身上,而旧的镜像上的这个名称则被取消,从而成为了 `<none>`。除了 `docker pull` 可能导致这种情况,`docker build` 也同样可以导致这种现象。由于新旧镜像同名,旧镜像名称被取消,从而出现仓库名、标签均为 `<none>` 的镜像。这类无标签镜像也被称为 **虚悬镜像(dangling image)** ,可以用下面的命令专门显示这类镜像: ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls </span><span class="pun">-</span><span class="pln">f dangling</span><span class="pun">=</span><span class="pln">true</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">REPOSITORY TAG IMAGE ID CREATED SIZE</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pun"><</span><span class="pln">none</span><span class="pun">></span><span class="pln"> </span><span class="pun"><</span><span class="pln">none</span><span class="pun">></span><span class="pln"> </span><span class="lit">00285df0df87</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">342</span><span class="pln"> MB</span></code></li> </ol> ``` 一般来说,虚悬镜像已经失去了存在的价值,是可以随意删除的,可以用下面的命令删除。 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image prune</span></code></li></ol> ``` ### 中间层镜像 为了加速镜像构建、重复利用资源,Docker 会利用 **中间层镜像**。所以在使用一段时间后,可能会看到一些依赖的中间层镜像。默认的 `docker image ls` 列表中只会显示顶层镜像,如果希望显示包括中间层镜像在内的所有镜像的话,需要加 `-a` 参数。 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls </span><span class="pun">-</span><span class="pln">a</span></code></li></ol> ``` 这样会看到很多无标签的镜像,与之前的虚悬镜像不同,这些无标签的镜像很多都是中间层镜像,是其它镜像所依赖的镜像。这些无标签镜像不应该删除,否则会导致上层镜像因为依赖丢失而出错。实际上,这些镜像也没必要删除,因为之前说过,相同的层只会存一遍,而这些镜像是别的镜像的依赖,因此并不会因为它们被列出来而多存了一份,无论如何你也会需要它们。只要删除那些依赖它们的镜像后,这些依赖的中间层镜像也会被连带删除。 ### 列出部分镜像 不加任何参数的情况下,`docker image ls` 会列出所有顶级镜像,但是有时候我们只希望列出部分镜像。`docker image ls` 有好几个参数可以帮助做到这个事情。 根据仓库名列出镜像 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls ubuntu</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">REPOSITORY TAG IMAGE ID CREATED SIZE</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">ubuntu </span><span class="lit">16.04</span><span class="pln"> f753707788c5 </span><span class="lit">4</span><span class="pln"> weeks ago </span><span class="lit">127</span><span class="pln"> MB</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">ubuntu latest f753707788c5 </span><span class="lit">4</span><span class="pln"> weeks ago </span><span class="lit">127</span><span class="pln"> MB</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">ubuntu </span><span class="lit">14.04</span><span class="pln"> </span><span class="lit">1e0c3dd64ccd</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> weeks ago </span><span class="lit">188</span><span class="pln"> MB</span></code></li> </ol> ``` 列出特定的某个镜像,也就是说指定仓库名和标签 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls ubuntu</span><span class="pun">:</span><span class="lit">16.04</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">REPOSITORY TAG IMAGE ID CREATED SIZE</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">ubuntu </span><span class="lit">16.04</span><span class="pln"> f753707788c5 </span><span class="lit">4</span><span class="pln"> weeks ago </span><span class="lit">127</span><span class="pln"> MB</span></code></li> </ol> ``` 除此以外,`docker image ls` 还支持强大的过滤器参数 `--filter`,或者简写 `-f`。之前我们已经看到了使用过滤器来列出虚悬镜像的用法,它还有更多的用法。比如,我们希望看到在 `mongo:3.2` 之后建立的镜像,可以用下面的命令: ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls </span><span class="pun">-</span><span class="pln">f since</span><span class="pun">=</span><span class="pln">mongo</span><span class="pun">:</span><span class="lit">3.2</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">REPOSITORY TAG IMAGE ID CREATED SIZE</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">redis latest </span><span class="lit">5f515359c7f8</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">183</span><span class="pln"> MB</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">nginx latest </span><span class="lit">05a60462f8ba</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> days ago </span><span class="lit">181</span><span class="pln"> MB</span></code></li> </ol> ``` 想查看某个位置之前的镜像也可以,只需要把 `since` 换成 `before` 即可。 此外,如果镜像构建时,定义了 `LABEL`,还可以通过 `LABEL` 来过滤。 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls </span><span class="pun">-</span><span class="pln">f label</span><span class="pun">=</span><span class="pln">com</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="pln">version</span><span class="pun">=</span><span class="lit">0.1</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pun">...</span></code></li> </ol> ``` ### 以特定格式显示 默认情况下,`docker image ls` 会输出一个完整的表格,但是我们并非所有时候都会需要这些内容。比如,刚才删除虚悬镜像的时候,我们需要利用 `docker image ls` 把所有的虚悬镜像的 ID 列出来,然后才可以交给 `docker image rm` 命令作为参数来删除指定的这些镜像,这个时候就用到了 `-q` 参数。 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls </span><span class="pun">-</span><span class="pln">q</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">5f515359c7f8</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">05a60462f8ba</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">fe9198c04d62</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">00285df0df87</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">f753707788c5</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">f753707788c5</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">1e0c3dd64ccd</span></code></li> </ol> ``` `--filter` 配合 `-q` 产生出指定范围的 ID 列表,然后送给另一个 `docker` 命令作为参数,从而针对这组实体成批的进行某种操作的做法在 Docker 命令行使用过程中非常常见,不仅仅是镜像,将来我们会在各个命令中看到这类搭配以完成很强大的功能。因此每次在文档看到过滤器后,可以多注意一下它们的用法。 另外一些时候,我们可能只是对表格的结构不满意,希望自己组织列;或者不希望有标题,这样方便其它程序解析结果等,这就用到了 [Go 的模板语法](https://gohugo.io/templates/go-templates/)。 比如,下面的命令会直接列出镜像结果,并且只包含镜像ID和仓库名: ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls </span><span class="pun">--</span><span class="pln">format </span><span class="str">"{{.ID}}: {{.Repository}}"</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">5f515359c7f8</span><span class="pun">:</span><span class="pln"> redis</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">05a60462f8ba</span><span class="pun">:</span><span class="pln"> nginx</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">fe9198c04d62</span><span class="pun">:</span><span class="pln"> mongo</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">00285df0df87</span><span class="pun">:</span><span class="pln"> </span><span class="pun"><</span><span class="pln">none</span><span class="pun">></span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">f753707788c5</span><span class="pun">:</span><span class="pln"> ubuntu</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">f753707788c5</span><span class="pun">:</span><span class="pln"> ubuntu</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">1e0c3dd64ccd</span><span class="pun">:</span><span class="pln"> ubuntu</span></code></li> </ol> ``` 或者打算以表格等距显示,并且有标题行,和默认一样,不过自己定义列: ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">$ docker image ls </span><span class="pun">--</span><span class="pln">format </span><span class="str">"table {{.ID}}\t{{.Repository}}\t{{.Tag}}"</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">IMAGE ID REPOSITORY TAG</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">5f515359c7f8</span><span class="pln"> redis latest</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">05a60462f8ba</span><span class="pln"> nginx latest</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">fe9198c04d62 mongo </span><span class="lit">3.2</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">00285df0df87</span><span class="pln"> </span><span class="pun"><</span><span class="pln">none</span><span class="pun">></span><span class="pln"> </span><span class="pun"><</span><span class="pln">none</span><span class="pun">></span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">f753707788c5 ubuntu </span><span class="lit">16.04</span></code></li> <li class="l1"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="pln">f753707788c5 ubuntu latest</span></code></li> <li class="l"><code class="pcalibre10 pcalibre11 pcalibre9"><span class="lit">1e0c3dd64ccd</span><span class="pln"> ubuntu </span><span class="lit">14.04</span></code></li> </ol> ```