mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-12 04:41:17 +00:00
Fix and update
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
## 列出镜像
|
||||
|
||||
## 列出镜像
|
||||
|
||||
在下载了镜像后,我们可以使用 `docker image ls` 命令列出本地主机上的镜像。
|
||||
|
||||
### 基本用法
|
||||
|
||||
查看本地已下载的镜像:
|
||||
@@ -19,6 +23,8 @@ ubuntu noble 329ed837d508 3 days ago 78MB
|
||||
|
||||
### 输出字段说明
|
||||
|
||||
`docker image ls` 命令默认输出的列表包含仓库名、标签、镜像 ID、创建时间和占用空间等信息。
|
||||
|
||||
| 字段 | 说明 |
|
||||
|------|------|
|
||||
| **REPOSITORY** | 仓库名 |
|
||||
@@ -35,6 +41,8 @@ ubuntu noble 329ed837d508 3 days ago 78MB
|
||||
|
||||
### 理解镜像大小
|
||||
|
||||
Docker 镜像的大小可能与我们通常理解的文件大小有所不同,这涉及到分层存储的概念。
|
||||
|
||||
#### 本地大小 vs Hub 显示大小
|
||||
|
||||
| 位置 | 显示大小 | 说明 |
|
||||
@@ -58,6 +66,8 @@ ubuntu:24.04 nginx:latest redis:latest
|
||||
|
||||
#### 查看实际空间占用
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
$ docker system df
|
||||
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
|
||||
@@ -71,10 +81,15 @@ Build Cache 0 0 0B 0B
|
||||
|
||||
### 过滤镜像
|
||||
|
||||
随着本地镜像数量的增加,我们需要更有效的方式来查找特定的镜像。Docker 提供了多种过滤方式。
|
||||
|
||||
#### 按仓库名过滤
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
## 列出所有 ubuntu 镜像
|
||||
|
||||
$ docker images ubuntu
|
||||
REPOSITORY TAG IMAGE ID SIZE
|
||||
ubuntu 24.04 329ed837d508 78MB
|
||||
@@ -84,6 +99,8 @@ ubuntu 22.04 a1b2c3d4e5f6 72MB
|
||||
|
||||
#### 按仓库名和标签过滤
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
$ docker images ubuntu:24.04
|
||||
REPOSITORY TAG IMAGE ID SIZE
|
||||
@@ -102,12 +119,15 @@ ubuntu 24.04 329ed837d508 78MB
|
||||
|
||||
```bash
|
||||
## 列出 nginx 之后创建的镜像
|
||||
|
||||
$ docker images -f since=nginx:latest
|
||||
|
||||
## 列出所有带 latest 标签的镜像
|
||||
|
||||
$ docker images -f reference='*:latest'
|
||||
|
||||
## 列出带特定 LABEL 的镜像
|
||||
|
||||
$ docker images -f label=maintainer=example@email.com
|
||||
```
|
||||
|
||||
@@ -115,6 +135,8 @@ $ docker images -f label=maintainer=example@email.com
|
||||
|
||||
### 虚悬镜像(Dangling Images)
|
||||
|
||||
在镜像列表里,你可能会看到一些仓库名和标签都为 `<none>` 的镜像,这类镜像被称为虚悬镜像。
|
||||
|
||||
#### 什么是虚悬镜像
|
||||
|
||||
仓库名和标签都显示为 `<none>` 的镜像:
|
||||
@@ -132,11 +154,15 @@ REPOSITORY TAG IMAGE ID SIZE
|
||||
|
||||
#### 处理虚悬镜像
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
## 列出虚悬镜像
|
||||
|
||||
$ docker images -f dangling=true
|
||||
|
||||
## 删除虚悬镜像
|
||||
|
||||
$ docker image prune
|
||||
```
|
||||
|
||||
@@ -144,8 +170,12 @@ $ docker image prune
|
||||
|
||||
### 中间层镜像
|
||||
|
||||
除了虚悬镜像,`docker image ls` 默认列出的只是顶层镜像。还有一种镜像是为了加速镜像构建、重复利用资源而存在的中间层镜像。
|
||||
|
||||
#### 查看所有镜像(包含中间层)
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
$ docker images -a
|
||||
```
|
||||
@@ -158,8 +188,12 @@ $ docker images -a
|
||||
|
||||
### 格式化输出
|
||||
|
||||
为了配合脚本使用或展示更关注的信息,我们可以使用 `--format` 参数来自定义输出格式。
|
||||
|
||||
#### 只输出 ID
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
$ docker images -q
|
||||
5f515359c7f8
|
||||
@@ -171,20 +205,26 @@ $ docker images -q
|
||||
|
||||
```bash
|
||||
## 删除所有镜像
|
||||
|
||||
$ docker rmi $(docker images -q)
|
||||
|
||||
## 删除所有 redis 镜像
|
||||
|
||||
$ docker rmi $(docker images -q redis)
|
||||
```
|
||||
|
||||
#### 显示完整 ID
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
$ docker images --no-trunc
|
||||
```
|
||||
|
||||
#### 显示摘要
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
$ docker images --digests
|
||||
REPOSITORY TAG DIGEST IMAGE ID
|
||||
@@ -197,12 +237,14 @@ nginx latest sha256:b4f0e0bdeb5... e43d811ce2f4
|
||||
|
||||
```bash
|
||||
## 只显示 ID 和仓库名
|
||||
|
||||
$ docker images --format "{{.ID}}: {{.Repository}}"
|
||||
5f515359c7f8: redis
|
||||
05a60462f8ba: nginx
|
||||
329ed837d508: ubuntu
|
||||
|
||||
## 表格形式(带标题)
|
||||
|
||||
$ docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
|
||||
REPOSITORY TAG SIZE
|
||||
redis latest 183MB
|
||||
@@ -226,14 +268,19 @@ ubuntu 24.04 78MB
|
||||
|
||||
### 常用命令组合
|
||||
|
||||
运行以下命令:
|
||||
|
||||
```bash
|
||||
## 列出所有镜像及其大小,按大小排序(需要系统 sort 命令)
|
||||
|
||||
$ docker images --format "{{.Size}}\t{{.Repository}}:{{.Tag}}" | sort -h
|
||||
|
||||
## 查找大于 500MB 的镜像
|
||||
|
||||
$ docker images --format "{{.Size}}\t{{.Repository}}:{{.Tag}}" | grep -E "^[0-9]+GB|^[5-9][0-9]{2}MB"
|
||||
|
||||
## 导出镜像列表
|
||||
|
||||
$ docker images --format "{{.Repository}}:{{.Tag}}" > images.txt
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user