目錄

docker rmi 指令:刪除映像檔與標籤(-f、image prune)

docker rmi(別名 docker image rm)移除映像檔標籤或未使用的映像檔;映像檔被容器使用時,通常會遇到 conflict。映像檔與硬碟空間的清理方式見 清理硬碟空間。

語法

docker rmi [參數] 映像檔 [映像檔...]
docker image rm [參數] 映像檔 [映像檔...]

參數

參數說明
-f, --force強制移除映像檔
--no-prune不刪除沒有標籤的父映像檔
--platform 字串只移除指定的平台版本,例如 linux/amd64

docker image prune 的參數:

參數說明
-a, --all移除所有未使用的映像檔,而不只 dangling 映像檔
--filter 條件提供篩選條件,例如 until=<timestamp>
-f, --force不提示確認

範例

以下使用的映像檔包括 alpine、demo/app、redis 和 registry。有些標籤指向同一個映像檔 ID。

移除標籤與遇到 conflict

$ docker images
IMAGE                                                                            ID             DISK USAGE   CONTENT SIZE   EXTRA
alpine:3.23                                                                      ff7f0c727dd7       26.6MB         8.21MB        
alpine@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0   85fe1e81d675         13MB         3.94MB        
demo/app:1.0                                                                     ff7f0c727dd7       26.6MB         8.21MB        
demo/app:2.0                                                                     ff7f0c727dd7       26.6MB         8.21MB        
localhost:5000/tools/alpine:3.23                                                 ff7f0c727dd7       26.6MB         8.21MB        
localhost:5000/tools/alpine:latest                                               ff7f0c727dd7       26.6MB         8.21MB        
redis:8-alpine                                                                   0bf6577d7774        161MB         39.6MB   U    
registry:3                                                                       01cd312e6702       86.9MB         20.7MB   U    

$ docker rmi demo/app:1.0
Untagged: demo/app:1.0

$ docker rmi redis:8-alpine
Error response from daemon: conflict: unable to delete redis:8-alpine (must be forced) - container 0663b06bc225 is using its referenced image 0bf6577d7774
  • demo/app:1.0 移除後回報 Untagged,因為同一個映像檔 ID 還有其他標籤。
  • redis:8-alpine 正被容器使用,直接刪除會收到 conflict,錯誤也指出容器 ID 和映像檔 ID。
  • EXTRA 欄位的 U 標示該映像檔正在使用中。

-f 強制移除標籤

$ docker rmi -f redis:8-alpine
Untagged: redis:8-alpine

$ docker ps -a --filter name=uses-redis
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS      NAMES
0663b06bc225   0bf6577d7774   "docker-entrypoint.s…"   3 seconds ago   Up 3 seconds   6379/tcp   uses-redis

$ docker rmi nosuch:image
Error response from daemon: No such image: nosuch:image
  • -f 成功移除了 redis:8-alpine 標籤;接著列出的容器仍在執行,顯示強制移除標籤不會停止容器。
  • 刪除不存在的映像檔名稱會回報 No such image。
  • 強制移除後,映像檔可能以 <none> 標籤留在本機;可用 docker system df -v 檢視詳細用量。

清除 dangling 與所有未使用映像檔

$ docker image prune -f
Total reclaimed space: 0B

$ docker image prune -a -f
Deleted Images:
untagged: alpine:3.23
untagged: alpine@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0
deleted: sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0
deleted: sha256:1f3591b8a02ea153f41c5bba878ad477f63ab3d19349762cb77504db02a23e15
deleted: sha256:c3709935ef4cf35769588b317a43fa75b70a7ee51af2d449069ba66e956a0f44
untagged: localhost:5000/tools/alpine:3.23
untagged: localhost:5000/tools/alpine:latest
untagged: demo/app:2.0
deleted: sha256:ff7f0c727dd796aa36787bfcd9a64bf35a6ce72a73451b3359fde849b6703d63
deleted: sha256:3d67941fed5ae436b2bb9791cb3f50a252441c7706bbc5b4e738936df8ff8306
deleted: sha256:74a5b9d36e2dbe5329330986093322eb7619d9d5ebbc916392cf686d0e7381ce
deleted: sha256:8793abc479381bafd16c9d538b01c5a8dd7eef6aa10a737f2ad828ac73cb80e9
deleted: sha256:6d423cc7d2471ec6b5d77eaa820bc0808c1213c56c6fb88ccab237a486ecab64

Total reclaimed space: 26.7MB

$ docker images
IMAGE        ID             DISK USAGE   CONTENT SIZE   EXTRA
registry:3   01cd312e6702       86.9MB         20.7MB   U    
  • 不加 -a 的 image prune 只移除 dangling 映像檔;此實測沒有可清除項目,所以回收 0B。
  • 加上 -a 後會移除所有未被容器使用的映像檔,包括仍有標籤的映像檔;仍在使用的 registry:3 留下。
  • Docker 29 預設較少留下 dangling 映像檔,因此只跑一般 image prune 常會看到沒有回收空間;要清除所有未使用的映像檔可用 -a。

檢視 <none> 映像檔並用 system prune 清理

$ 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

$ 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
  • docker system df -v 顯示 <none>:<none> 的映像檔 ID 0bf6577d7774,沒有容器使用,但仍占 161MB,其中 147.6MB 是獨有空間。
  • docker system prune -f 刪除此映像檔及其相關資料,回收 147.6MB;之後 Images 用量從 420.2MB 降到 272.6MB。
  • system prune 會清理未使用的其他 Docker 資料;若只要處理映像檔,使用 docker image prune。

相關章節

動手練習

練習:先嘗試移除 redis:8-alpine,再用 -f 移除它的標籤,並確認使用該映像檔的 uses-redis 容器仍在執行。

答案
docker rmi redis:8-alpine
docker rmi -f redis:8-alpine
docker ps -a --filter name=uses-redis

常見問題

docker rmi 和 docker image rm 是不同指令嗎?

不是。docker rmi、docker image rm 和 docker image remove 是同一個指令的別名。

映像檔被執行中的容器使用時,可以直接刪除嗎?

一般刪除會回報 conflict,指出使用該映像檔的容器。加上 -f 可移除映像檔的標籤;實測中容器仍在執行。

docker image prune 和 docker image prune -a 有什麼差別?

不加 -a 只清除 dangling 映像檔;加上 -a 則清除所有未被容器使用的映像檔。Docker 29 的實測中,預設 prune 回收 0B,而 -a 刪除了未使用的映像檔。

用 -f 刪除被容器使用的映像檔後,還能回收它嗎?

docker system df -v 可看到該映像檔以 <none> 標籤留存;實測 docker system prune -f 將它刪除並回收 147.6MB。

更新於 2026年10月26日