目錄

docker system 指令:查看磁碟用量、清理資料與監看事件

docker system 可檢視 Docker 資料用量、清理未使用資料,並取得系統資訊與事件;清理時可參考清理硬碟空間了解如何管理 Docker 占用的空間。

語法

docker system df [OPTIONS]
docker system prune [OPTIONS]
docker info [OPTIONS]
docker events [OPTIONS]

docker info 與 docker system info、docker events 與 docker system events 分別是相同指令的別名。

參數

指令參數說明
docker system df--format 範本使用自訂範本輸出;可用 table、json 或 Go 範本
docker system df-v, --verbose顯示空間用量的詳細資訊
docker system prune-a, --all清除所有未使用映像檔,而不只清除懸空映像檔
docker system prune--filter 條件依條件篩選清理對象,例如 label=<key>=<value>
docker system prune-f, --force不提示確認
docker system prune--volumes清理匿名磁碟區
docker info-f, --format 範本使用自訂範本輸出;可用 json 或 Go 範本
docker events-f, --filter 條件依指定條件篩選輸出事件
docker events--format 範本使用自訂範本輸出;可用 json 或 Go 範本
docker events--since 時間顯示指定時間之後建立的事件
docker events--until 時間持續串流事件直到指定時間

範例

查看磁碟用量與詳細明細

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          5         3         420.2MB   239.7MB (57%)
Containers      3         3         139.3kB   0B (0%)
Local Volumes   1         1         3.854MB   0B (0%)
Build Cache     0         0         0B        0B

$ docker system df -v | head -20
Images space usage:

REPOSITORY   TAG           IMAGE ID       CREATED         SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
myweb        snapshot      56e3f92534ca   7 seconds ago   92.1MB    0B            92.08MB       0
nginx        1.30-alpine   47928bf67eb6   3 days ago      93.5MB    12.94MB       80.6MB        1
registry     3             01cd312e6702   3 days ago      86.9MB    12.94MB       73.93MB       1
<none>       <none>        0bf6577d7774   3 days ago      161MB     12.93MB       147.6MB       0
alpine       3.23          ff7f0c727dd7   3 days ago      13MB      12.93MB       86.86kB       1

Containers space usage:

CONTAINER ID   IMAGE               COMMAND                  LOCAL VOLUMES   SIZE      CREATED          STATUS          NAMES
0c009f73010b   alpine:3.23         "sleep 600"              0               4.1kB     42 seconds ago   Up 41 seconds   small
63b9a2f9c63f   nginx:1.30-alpine   "/docker-entrypoint.…"   0               115kB     44 seconds ago   Up 43 seconds   web
35357aad74b1   registry:3          "/entrypoint.sh /etc…"   1               20.5kB    2 minutes ago    Up 2 minutes    registry

Local Volumes space usage:

VOLUME NAME                                                        LINKS     SIZE
21ea99090ddead0461eb5a6efd670e1d9cf58fe9c23d5f8618db5868fb6170aa   1         3.854MB
  • 一般摘要分別列出映像檔、容器、本機磁碟區與建置快取的總數、作用中數量、大小和可回收大小。
  • -v 會列出各映像檔與容器的用量;映像檔明細還包含共享大小、專屬大小與使用它的容器數。
  • head -20 只保留輸出的前 20 行,因此這段明細沒有顯示後續內容。

清理未使用資料

$ docker system prune -f
Deleted Images:
untagged: sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
deleted: sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
deleted: sha256:16b124a2d3eacd620d917ead6fd26cbf1f1d7117fa94122398971931cfe2d403
deleted: sha256:f79a7876c6644b273e4eec899344e51c66959237a2f71188db8cb98d7311521c

Total reclaimed space: 147.6MB

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          4         3         272.6MB   92.08MB (33%)
Containers      3         3         139.3kB   0B (0%)
Local Volumes   1         1         3.854MB   0B (0%)
Build Cache     0         0         0B        0B
  • -f 讓清理指令略過確認提示。範例刪除未使用的映像檔資料,回報回收 147.6MB。
  • 清理前後容器數都是 3;映像檔由 5 個變成 4 個,總大小由 420.2MB 降至 272.6MB。
  • --all 可清理所有未使用映像檔,--volumes 可清理匿名磁碟區;請先確認需要保留的資料。

用格式範本取得系統資訊

$ docker info --format '{{.ServerVersion}} {{.Driver}} {{.Containers}} containers, {{.Images}} images'
29.9.2 overlayfs 3 containers, 5 images
  • --format 讓輸出只包含範本指定的伺服器版本、儲存驅動程式、容器數與映像檔數。
  • 範例顯示伺服器版本為 29.9.2、儲存驅動程式為 overlayfs。

依時間與條件查看事件

$ timeout 3 docker events --since 30s --format '{{.Type}} {{.Action}} {{.Actor.Attributes.name}}' ; true
container top web
container top web
container kill busy
network disconnect bridge
container die busy
container destroy busy
image save sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951
image save sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951
image untag nginx:1.30-alpine
image load sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951
container exec_create: sh -c echo hello > /usr/share/nginx/html/hi.txt web
container exec_start: sh -c echo hello > /usr/share/nginx/html/hi.txt web
container exec_die web
container export web
image tag myweb:snapshot
image import sha256:56e3f92534ca8c5aedd8de576ae2433f1864d3d3b51de847a8dc8dd7d503da4d
container create sharp_gould
container attach sharp_gould
network connect bridge
container start sharp_gould
network disconnect bridge
container die sharp_gould
container destroy sharp_gould
container prune <no value>
network prune <no value>
image untag sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
image delete sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
builder prune <no value>

$ docker events --since 10s --until 0s --filter type=container --format '{{.Action}} {{.Actor.Attributes.name}}'
prune <no value>
create evt
attach evt
start evt
die evt
destroy evt
  • 第一個範例從最近 30 秒的事件開始串流,格式只輸出事件類型、動作與名稱;timeout 3 限制執行時間。
  • 第二個範例用 --filter type=container 限定容器事件,並以 --since、--until 設定時間範圍。
  • prune 事件的名稱欄位顯示 <no value>,因為該事件沒有這個範本所讀取的名稱值。

相關章節

動手練習

練習:只列出最近 10 秒內的容器事件,輸出事件動作與容器名稱。

答案
docker events --since 10s --until 0s --filter type=container --format '{{.Action}} {{.Actor.Attributes.name}}'

常見問題

docker system df -v 的 UNIQUE SIZE 是什麼?

它列出映像檔的專屬大小;輸出也會另外列出 SIZE 與 SHARED SIZE。像 alpine:3.23 的 SIZE 是 13MB、SHARED SIZE 是 12.93MB、UNIQUE SIZE 是 86.86kB。

docker system prune 會刪除使用中的映像檔嗎?

範例中它刪除的是沒有容器使用的懸空映像檔,回報回收 147.6MB;清理後仍有 3 個作用中容器。--all 會把清理範圍擴大到所有未使用映像檔,--volumes 則會清理匿名磁碟區。

docker events 可以只看容器事件嗎?

可以,使用 --filter type=container。搭配 --since 和 --until 可限定事件時間範圍,--format 可選擇輸出的事件欄位。

如何只從 docker info 取得 Docker 版本和映像檔數量?

用 docker info --format 指定 Go 範本欄位;範例輸出為 29.9.2 overlayfs 3 containers, 5 images。

更新於 2026年10月26日