目錄
docker compose down 指令:停止並刪除容器、網路與 Volume
docker compose down 停止並刪除 Compose 專案的容器與網路。
語法
docker compose down [參數] [服務名稱...]
| 參數 | 說明 |
|---|---|
-v, --volumes | 同時刪除 compose.yaml 中宣告的具名 Volume 與匿名 Volume(資料會消失) |
--rmi local|all | 同時刪除映像檔。local 只刪除沒有自訂 tag 的(Compose 建置的),all 刪除所有服務用到的 |
--remove-orphans | 同時刪除 compose.yaml 中已經不存在的服務的容器 |
-t, --timeout 秒數 | 停止容器時等待的秒數(預設 10 秒) |
--dry-run | 只顯示會做什麼 |
範例
使用 docker compose up 的範例專案:
$ docker compose down
[+] down 4/4
✔ Container ref-compose-worker-1 Removed 0.0s
✔ Container ref-compose-web-1 Removed 0.2s
✔ Container ref-compose-cache-1 Removed 0.2s
✔ Network ref-compose_default Removed 0.0s
$ docker volume ls
DRIVER VOLUME NAME
local ref-compose_cache_data
$ docker compose up -d
[+] up 4/4
✔ Network ref-compose_default Created 0.0s
✔ Container ref-compose-worker-1 Started 0.3s
✔ Container ref-compose-cache-1 Started 0.6s
✔ Container ref-compose-web-1 Started 0.8s
$ docker compose down -v -t 1
[+] down 5/5
✔ Container ref-compose-web-1 Removed 0.1s
✔ Container ref-compose-worker-1 Removed 0.0s
✔ Container ref-compose-cache-1 Removed 0.2s
✔ Volume ref-compose_cache_data Removed 0.0s
✔ Network ref-compose_default Removed 0.0s
$ docker volume ls
DRIVER VOLUME NAME
- 不加參數的
down:刪除 3 個容器和網路,Volumeref-compose_cache_data被保留。 down -v -t 1:多刪除了 Volume,Redis 的資料也就消失了。-t 1讓每個容器最多等 1 秒就強制停止,練習環境中可以加快速度;正式環境請保留預設值,讓程式有時間正常結束。
down、stop、rm 怎麼選
| 指令 | 容器 | 網路 | Volume | 用途 |
|---|---|---|---|---|
docker compose stop | 停止,保留 | 保留 | 保留 | 暫停服務,之後 start 恢復 |
docker compose rm | 刪除已停止的 | 保留 | 保留 | 很少單獨使用 |
docker compose down | 停止並刪除 | 刪除 | 保留 | 一般的關閉,資料安全 |
docker compose down -v | 停止並刪除 | 刪除 | 刪除 | 完全重來,例如重新初始化資料庫 |
資料庫的初始化腳本與密碼變數只在 Volume 是空的時候生效,所以「改了設定想重新初始化」時才需要
down -v,原因見 MySQL。
相關章節
動手練習
練習:用一個指令停止並刪除專案的容器、網路,以及 Compose 自己建置的映像檔,但保留資料。
答案
docker compose down --rmi local
不加 -v,Volume 就會保留。
常見問題
docker compose down 會刪除資料庫的資料嗎?
不加參數**不會**,Volume 會保留,下次 up 資料還在。加上 -v 才會刪除 compose.yaml 中宣告的 Volume。
down 之後映像檔還在嗎?
還在。要刪除 Compose 建置的映像檔用 --rmi local,連下載的映像檔也刪除用 --rmi all。
只想暫時停止,不想刪除容器?
用 docker compose stop,之後 docker compose start 恢復。
更新於 2026年10月14日