目錄

Docker 常見錯誤訊息大全:原因與解法(附實際輸出)

這一頁把 Docker 常見的錯誤訊息整理在一起,標題就是錯誤訊息的關鍵字,可以用瀏覽器的搜尋功能(Ctrl+F)直接找。每個錯誤都附上在 Docker 29 實際重現的輸出、原因與解法。

找不到你的錯誤時,排查的基本步驟是:

  1. 看錯誤訊息最後一段的關鍵字。
  2. docker ps -a 看容器狀態與結束代碼。
  3. docker logs 容器名稱 看程式自己的輸出。
  4. 詳細的除錯方法見 查看日誌與除錯。

連線與權限

failed to connect to the docker API / Cannot connect to the Docker daemon

$ docker run --rm --entrypoint docker docker:29.9-cli -H unix:///var/run/docker.sock ps
failed to connect to the docker API at unix:///var/run/docker.sock; check if the path is correct and if the daemon is running: dial unix /var/run/docker.sock: connect: no such file or directory

舊版的訊息是 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?,意思相同:docker 指令(用戶端)找不到 Docker Engine(伺服器)。

解法:

  • Windows / macOS:確認 Docker Desktop 已經啟動,系統匣的鯨魚圖示停止轉動、狀態是 Engine running。
  • Linux:啟動 Docker 服務:sudo systemctl start docker,並設定開機啟動:sudo systemctl enable docker。
  • 檢查是否設定了 DOCKER_HOST 環境變數,或切換到了別的 context:docker context ls。

permission denied while trying to connect to the docker API

$ docker run --rm -u 1000:1000 -v /var/run/docker.sock:/var/run/docker.sock docker:29.9-cli docker ps
permission denied while trying to connect to the docker API at unix:///var/run/docker.sock

在 Linux 上,/var/run/docker.sock 只有 root 和 docker 群組可以存取。一般使用者執行 docker 就會出現這個錯誤。

解法:把使用者加入 docker 群組,然後重新登入(或執行 newgrp docker)才會生效:

sudo usermod -aG docker $USER

加入 docker 群組等同給予 root 權限,原因見 安全最佳實務。想避免的話可以使用 Rootless 模式。

連接埠與名稱

port is already allocated

$ docker run -d --name web1 -p 8080:80 nginx:1.30-alpine
7a72d42f052dfebded509314b1975c72fbb1fcbf0c97099d6d60c9dedbcad8a1

$ docker run -d --name web2 -p 8080:80 nginx:1.30-alpine
6bcca59a67e5a5adbab1da7b2dbb7ef92f1df1f6e7680462a22541fd96f0ac9f
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint web2 (3a269ae1baae56a10b06552ba76fff69b3414885b2fa7a737344e081b4d5acd5): Bind for 0.0.0.0:8080 failed: port is already allocated

Run 'docker run --help' for more information

主機的 8080 已經被另一個容器使用(這裡是 web1)。注意:錯誤發生時 web2 其實已經建立了,只是沒有啟動(狀態是 Created),所以修正後直接再執行同一個指令,會遇到下一個錯誤「名稱已被使用」。

解法:

  • 換一個主機連接埠:-p 8081:80。
  • 或停止佔用的容器:docker ps 找出是誰在用 8080,然後 docker stop。
  • 刪除建立失敗的容器:docker rm web2。

address already in use

$ docker run -d --name web3 -p 9090:80 nginx:1.30-alpine
2c4649bbf85211d1ef6852c95fad52075b63d8d77e3fcd7c74db0bc7d15fbd8f
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint web3 (38386ef2562f999706b1d2b00eac0e48125eb006916dc857832ce18d36fe44d6): failed to bind host port 0.0.0.0:9090/tcp: address already in use

Run 'docker run --help' for more information

和上一個很像,但佔用連接埠的是主機上的其他程式(不是容器),例如本機已經安裝了 Nginx、MySQL,或開發伺服器正在執行。

解法:找出佔用的程式後關閉它,或換一個連接埠。

sudo lsof -i :9090          # Linux / macOS
netstat -ano | findstr 9090 # Windows

The container name "/web1" is already in use

$ docker run -d --name web1 -p 8081:80 nginx:1.30-alpine
docker: Error response from daemon: Conflict. The container name "/web1" is already in use by container "7a72d42f052dfebded509314b1975c72fbb1fcbf0c97099d6d60c9dedbcad8a1". You have to remove (or rename) that container to be able to reuse that name.

Run 'docker run --help' for more information

容器名稱不能重複,包括已停止的容器。

解法:

  • 刪除舊容器:docker rm -f web1。
  • 或換一個名稱。
  • 一次性執行的容器加上 --rm,結束後自動刪除,就不會留下同名容器。

映像檔

not found / manifest unknown / pull access denied

$ docker pull nginx:9.99
Error response from daemon: failed to resolve reference "docker.io/library/nginx:9.99": docker.io/library/nginx:9.99: not found

$ docker pull ngnix-official-not-real
Using default tag: latest
Error response from daemon: failed to resolve reference "docker.io/library/ngnix-official-not-real:latest": docker.io/library/ngnix-official-not-real:latest: not found

找不到映像檔。常見原因:

  • tag 打錯或不存在:nginx:9.99 這個版本不存在。到 Docker Hub 的 Tags 頁確認,或用 docker buildx imagetools inspect 映像檔 查詢。
  • 名稱打錯:ngnix。官方映像檔名稱沒有前綴,例如 nginx、postgres。
  • 私有映像檔沒有登入:先 docker login。

依 Docker 版本與 Registry 的不同,同樣的問題也可能顯示為 manifest unknown 或 pull access denied for ..., repository does not exist or may require 'docker login'。

repository name must be lowercase / invalid reference format

$ docker build -t MyApp .
ERROR: failed to build: invalid tag "MyApp": repository name must be lowercase

映像檔名稱只能使用小寫字母、數字和 -、_、.、/。把 MyApp 改成 myapp。名稱中有空格、多了冒號(例如 myapp::1.0)時,則會出現 invalid reference format。

toomanyrequests: You have reached your pull rate limit

未登入時從 Docker Hub 下載映像檔有次數限制,在短時間內大量拉取(例如 CI 每次都重新下載)就會遇到。這個錯誤沒有在本教學環境重現,錯誤文字以官方說明為準。

解法:

  • docker login 登入 Docker Hub 帳號,限制會放寬。
  • CI 中使用快取,或改用其他 Registry 的映像檔鏡像。
  • 本機已經有的映像檔不需要重新拉取,避免在指令中不必要地加 --pull always。

exec format error

映像檔的 CPU 架構和主機不同,例如在 Apple Silicon Mac 上建置的 arm64 映像檔放到 amd64 伺服器上執行。實際輸出與解法見 多平台映像檔。

容器啟動失敗

exec /entrypoint.sh: no such file or directory(檔案明明存在)

最常見於 Windows 使用者:shell 腳本用 Windows 的換行(CRLF)存檔。

$ docker build -q -t crlf-demo .
sha256:194d0a73ff66c601a1e7b1bf80417ae47c5de6e5e076415f17b483e642929fc2

$ docker run --rm crlf-demo
exec /entrypoint.sh: no such file or directory

$ od -c entrypoint.sh | head -n 2
0000000   #   !   /   b   i   n   /   s   h  \r  \n   e   c   h   o    
0000020   "   h   e   l   l   o       f   r   o   m       e   n   t   r

od -c 顯示第一行結尾是 \r \n。Linux 讀到的直譯器是 /bin/sh\r(多了一個看不見的 \r),這個程式當然不存在,所以出現 no such file or directory,非常誤導人。

解法:

  • 在編輯器把換行改成 LF(VS Code 右下角點 CRLF 改成 LF)。

  • Git 在 Windows 上預設會把換行轉成 CRLF,在專案根目錄加入 .gitattributes,讓腳本永遠使用 LF:

    *.sh text eol=lf
  • 或在 Dockerfile 中轉換:RUN sed -i 's/\r$//' /entrypoint.sh。

exec: "/entrypoint.sh": permission denied

$ docker build -q -t noexec-demo .
sha256:4f37fec89287104ccf6b509fef5c1836d2a1a65b36d858ff63b95fecc3340a0e

$ docker run --rm noexec-demo
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: "/entrypoint.sh": permission denied

Run 'docker run --help' for more information

腳本沒有執行權限。Windows 的檔案系統沒有 Linux 的執行權限,從 Windows 建置時特別容易遇到。

解法:在 Dockerfile 中加上執行權限:

COPY --chmod=755 entrypoint.sh /entrypoint.sh

或在 Linux / macOS 上 chmod +x entrypoint.sh 後再建置。

executable file not found in $PATH

$ docker run --rm alpine:3.23 bash
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: "bash": executable file not found in $PATH

Run 'docker run --help' for more information

容器裡沒有這個程式。alpine 映像檔沒有 bash,只有 sh。

解法:改用 sh,或在 Dockerfile 中安裝(RUN apk add --no-cache bash)。也常見於 CMD 寫錯程式名稱、或程式沒有被複製進映像檔。

容器啟動後馬上 Exited (0)

$ docker run -d --name u1 ubuntu:26.04
9ff7034514a469602b48cc1bb3758514b32af8de6c602ce1bee11cd85813671c

$ docker ps -a --filter name=u1 --format "table {{.Names}}\t{{.Status}}"
NAMES     STATUS
u1        Exited (0) 2 seconds ago

$ docker rm u1
u1

這不是錯誤:容器在主程序結束時就會停止。ubuntu 映像檔的預設指令是 bash,在背景執行(-d)且沒有終端機時,bash 讀不到輸入就結束了。

解法:

  • 想進去操作:docker run -it ubuntu:26.04 bash。
  • 想讓它一直執行:給它一個不會結束的程式,例如網頁伺服器;測試用可以 sleep infinity。
  • 結束代碼不是 0 時,看 docker logs 找程式的錯誤訊息。

Exited (137) 或 OOMKilled

結束代碼 137 表示容器被 SIGKILL 強制終止(128 + 9)。常見原因:

  • 記憶體不足:超過 --memory 限制或主機記憶體不足,被系統終止。用 docker inspect -f "{{.State.OOMKilled}}" 容器 確認,true 就是記憶體不足。
  • docker stop 等了 10 秒程式還沒結束,被強制終止,原因見 Node.js 應用 的 PID 1 實測。
  • docker kill 或 docker rm -f。

本教學的測試主機沒有啟用 swap 限制,記憶體不足的情況無法穩定重現,所以這一項沒有附上實際輸出。

cannot attach stdin to a TTY-enabled container because stdin is not a terminal / the input device is not a TTY

$ docker run -d --name a1 alpine:3.23 sleep 300
55637ca715821720e55fa1366d8df8813915bf12b7520f65b9e85922ea7c475c

$ docker exec -it a1 bash
cannot attach stdin to a TTY-enabled container because stdin is not a terminal

-t 要求配置終端機,但目前的輸入不是終端機。常見於:在腳本、CI、cron 中使用 docker exec -it,或用 <、| 把檔案傳給容器。

解法:非互動的情況拿掉 -t,只保留 -i(需要輸入時)或什麼都不加。例如還原資料庫要用 docker exec -i。

建置(docker build)

failed to read dockerfile: open Dockerfile: no such file or directory

$ docker build -t x .
#0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 2B done
#1 DONE 0.0s
ERROR: failed to build: failed to solve: failed to read dockerfile: open Dockerfile: no such file or directory

目前資料夾沒有 Dockerfile。

解法:

  • 確認在正確的資料夾執行,最後的 . 是建置內容(build context)的路徑。
  • 檔名必須是 Dockerfile(大小寫一致,沒有副檔名)。Windows 的記事本可能自動加上 .txt,檔名變成 Dockerfile.txt。
  • 檔名不同時用 -f 指定:docker build -f Dockerfile.dev .。

failed to calculate checksum ... not found(COPY 找不到檔案)

$ docker build -t copy-demo .

[+] Building 0.1s (6/6) FINISHED                                                                                                       docker:default
 => [internal] load build definition from Dockerfile                                                                                             0.0s
 => => transferring dockerfile: 88B                                                                                                              0.0s
 => WARN: CopyIgnoredFile: Attempting to Copy file "config.json" that is excluded by .dockerignore (line 2)                                      0.0s
 => [internal] load metadata for docker.io/library/alpine:3.23                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                0.0s
 => => transferring context: 52B                                                                                                                 0.0s
 => [internal] load build context                                                                                                                0.0s
 => => transferring context: 2B                                                                                                                  0.0s
 => CACHED [1/2] FROM docker.io/library/alpine:3.23@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0                      0.0s
 => => resolve docker.io/library/alpine:3.23@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0                             0.0s
 => ERROR [2/2] COPY config.json /app/config.json                                                                                                0.0s
------
 > [2/2] COPY config.json /app/config.json:
------

 1 warning found (use docker --debug to expand):
 - CopyIgnoredFile: Attempting to Copy file "config.json" that is excluded by .dockerignore (line 2)
Dockerfile:2
--------------------
   1 |     FROM alpine:3.23
   2 | >>> COPY config.json /app/config.json
   3 |
--------------------
ERROR: failed to build: failed to solve: failed to compute cache key: failed to calculate checksum of ref v1wocdfz4uxsgxp2q2kukkuqp::jww9anqv7tl0n3ksy
1tlwp8fv: "/config.json": not found

COPY 的檔案不在建置內容中。最上面的 WARN: CopyIgnoredFile 已經說明了原因:config.json 被 .dockerignore 排除了。

常見原因:

  • 檔案被 .dockerignore 排除(Docker 29 會先顯示警告)。
  • 路徑寫錯,或檔案在建置內容(docker build 最後的 .)之外。COPY ../file 不能存取上層資料夾。
  • 大小寫不一致:Windows 和 macOS 不分大小寫,Linux 分。

RUN ... did not complete successfully: exit code: 1

建置時某個 RUN 指令失敗。往上看 ------ 之間的輸出,那是該指令自己的錯誤訊息。例如 Python 應用 中 Django 的 collectstatic 因為沒有設定 STATIC_ROOT 而失敗。看不到完整輸出時加上 --progress plain。

Volume、網路

volume is in use

$ docker volume create data
data

$ docker run -d --name holder -v data:/data alpine:3.23 sleep 300
38eb68e3f9c7020fdb86ded25f1e73dc0a9fdcb932dea1353d04c4474e0e50d7

$ docker volume rm data
Error response from daemon: remove data: volume is in use - [38eb68e3f9c7020fdb86ded25f1e73dc0a9fdcb932dea1353d04c4474e0e50d7]

$ docker rm -f holder
holder

$ docker volume rm data
data

Volume 還被容器使用(包括已停止的容器),不能刪除。錯誤訊息中的 ID 就是使用它的容器。先刪除容器再刪除 Volume。

network ... not found

$ docker run --rm --network appnet alpine:3.23 true
docker: Error response from daemon: failed to set up container networking: network appnet not found

Run 'docker run --help' for more information

指定的網路不存在。先 docker network create appnet 建立,或用 docker network ls 確認名稱。Compose 專案的網路名稱會加上專案名稱前綴,例如 shop_default。

invalid mount config for type "bind": bind source path does not exist

用 --mount type=bind 掛載不存在的主機路徑時會出現。實際輸出與 -v 的差別見 Volume 與資料保存。

Docker Compose

yaml: ... did not find expected key / mapping values are not allowed

$ docker compose config
go-yaml load error in parser (while parsing a block mapping) at L3.C5-L6.C6: did not find expected key

YAML 的縮排錯誤。這個例子的 environment: 多縮排了一格。錯誤訊息中的 L3.C5-L6.C6 表示從第 3 行第 5 欄到第 6 行第 6 欄之間的區塊有問題。

解法:

  • 只用空白縮排,不要用 Tab。
  • 同一層的鍵要對齊。
  • 用 docker compose config 檢查,沒有錯誤時它會輸出整理後的完整設定。

depends on undefined service

$ docker compose up -d
service "app" depends on undefined service "db": invalid compose project

depends_on 寫了不存在的服務名稱,通常是打錯字,或服務被放進了 profiles 而沒有啟用。

The "DB_PASSWORD" variable is not set. Defaulting to a blank string.

$ docker compose config
time="2026-10-13T05:27:10Z" level=warning msg="The \"DB_PASSWORD\" variable is not set. Defaulting to a blank string."
name: c3
services:
  db:
    environment:
      POSTGRES_PASSWORD: ""
    image: postgres:18
    networks:
      default: null
networks:
  default:
    name: c3_default

這是警告而不是錯誤,但很危險:compose.yaml 用了 ${DB_PASSWORD},卻沒有在 .env 或環境變數中設定,於是被換成空字串,資料庫可能就用空密碼初始化了。

解法:在 .env 中設定變數,或寫成 ${DB_PASSWORD:?DB_PASSWORD is required},沒有設定時直接報錯停止,詳見 環境變數與 .env 檔。

dependency failed to start: container ... is unhealthy

$ docker compose up -d
[+] up 3/3
 ✔ Network c4_default Created                                                                                                                     0.0s
 ✘ Container c4-db-1  Error dependency db failed to start                                                                                         2.8s
 ✔ Container c4-app-1 Created                                                                                                                     0.0s
dependency failed to start: container c4-db-1 is unhealthy

depends_on 設定了 condition: service_healthy,而依賴的服務健康檢查一直失敗。這個例子故意讓健康檢查執行 false。

排查:

  • docker inspect --format "{{json .State.Health}}" 容器 查看每次健康檢查的輸出。
  • 檢查健康檢查指令本身是否正確,例如密碼、主機名稱。資料庫的健康檢查寫法見 MySQL 與 PostgreSQL。
  • 啟動很慢的服務,加大 start_period 和 retries。

硬碟空間

No space left on device

$ docker run --rm --tmpfs /data:size=1m alpine:3.23 dd if=/dev/zero of=/data/big bs=1M count=2
dd: error writing '/data/big': No space left on device
2+0 records in
1+0 records out
1048576 bytes (1.0MB) copied, 0.001369 seconds, 730.5MB/s

寫入的地方空間不足。這個例子用 1 MB 的 tmpfs 示範容器內的情況;實務上更常見的是主機上 Docker 的資料目錄滿了,建置或拉取映像檔時出現同樣的錯誤。

解法:

  • docker system df 看空間被什麼佔用,再依 清理硬碟空間 清理。
  • 檢查容器日誌是否太大,並設定日誌上限。
  • Docker Desktop 可以在 Settings → Resources 調大虛擬磁碟上限。

Windows 特有的錯誤

以下用 PowerShell 7.4 實際執行(為了在同一個環境中實測,用 PowerShell 官方映像檔在容器中執行 pwsh,所以指令前面多了 docker run ... pwsh -Command)。Windows PowerShell 5.1 的訊息文字略有不同,但原因相同。

Variable reference is not valid. ':' was not followed by a valid variable name character

$ docker run --rm -e NO_COLOR=1 -w /home/me/project mcr.microsoft.com/powershell:latest pwsh -NoProfile -Command '$PSStyle.OutputRendering="PlainText"; mkdir /home/me/project -Force | Out-Null; Set-Location /home/me/project; Write-Output "$(pwd)/site:/app"; Write-Output "${PWD}/site:/app"'

/home/me/project/site:/app
/home/me/project/site:/app

$ docker run --rm -e NO_COLOR=1 -w /home/me/project mcr.microsoft.com/powershell:latest pwsh -NoProfile -Command '$PSStyle.OutputRendering="PlainText"; Write-Output "$PWD:/app"'
ParserError:
Line |
   1 |  $PSStyle.OutputRendering="PlainText"; Write-Output "$PWD:/app"
     |                                                      ~~~~~
     | Variable reference is not valid. ':' was not followed by a valid
     | variable name character. Consider using ${} to delimit the name.

在 PowerShell 中寫 "$PWD:/app" 會出錯:PowerShell 把 $PWD: 當成特殊的變數語法。改寫成 "${PWD}:/app" 就正常。"$(pwd)/site:/app" 這種寫法在 PowerShell 中也能用(第一個指令的兩行輸出相同)。

完整的 Windows 寫法對照見 第一個容器。

The '<' operator is reserved for future use

$ docker run --rm -e NO_COLOR=1 -w /home/me/project mcr.microsoft.com/powershell:latest pwsh -NoProfile -Command '$PSStyle.OutputRendering="PlainText"; docker exec -i mysql mysql app < app.sql'
ParserError:
Line |
   1 |  … OutputRendering="PlainText"; docker exec -i mysql mysql app < app.sql
     |                                                                ~
     | The '<' operator is reserved for future use.

PowerShell 不支援 < 把檔案導入指令,所以教學中還原資料庫的 docker exec -i ... < backup.sql 在 PowerShell 中無法執行。

解法:先用 docker cp 把檔案複製進容器,再在容器內導入。這個方法在所有系統都通用,也避開了 PowerShell 5.1 的 > 會把檔案存成 UTF-16 編碼的問題。以 PostgreSQL 為例:

$ docker compose exec db psql -U app -c 'CREATE TABLE t (id int); INSERT INTO t VALUES (1),(2);'
CREATE TABLE
INSERT 0 2

$ docker compose exec db pg_dump -U app -Fc -f /tmp/app.dump app

$ docker compose cp db:/tmp/app.dump app.dump
 pgwin-db-1 Copying pgwin-db-1:/tmp/app.dump to app.dump
 pgwin-db-1 Copied pgwin-db-1:/tmp/app.dump to app.dump

$ ls -l app.dump
-rw-rw-rw-    1 root     root          1251 Oct 13 05:30 app.dump

$ docker compose exec db psql -U app -c 'DROP TABLE t;'
DROP TABLE

$ docker compose cp app.dump db:/tmp/restore.dump
 pgwin-db-1 Copying app.dump to pgwin-db-1:/tmp/restore.dump
 pgwin-db-1 Copied app.dump to pgwin-db-1:/tmp/restore.dump

$ docker compose exec db pg_restore -U app -d app /tmp/restore.dump

$ docker compose exec db psql -U app -c 'SELECT count(*) FROM t;'
 count
-------
     2
(1 row)

MySQL 的寫法見 MySQL 的備份一節。

重點回顧

  • 先看錯誤訊息最後一段的關鍵字,再用 docker ps -a、docker logs 確認狀態。
  • 連接埠被佔用、名稱衝突時,失敗的容器可能已經建立,記得 docker rm。
  • Windows 上的腳本要用 LF 換行並有執行權限,否則會出現誤導人的 no such file or directory 或 permission denied。
  • Compose 的問題先跑 docker compose config。
  • PowerShell 中用 ${PWD},並避免使用 < 導入檔案。

常見問題

錯誤訊息很長,應該看哪一段?

先看最後一段冒號後面的文字,那通常才是真正的原因,例如 port is already allocated、no such file or directory。前面的 Error response from daemon、failed to create task 只是說明錯誤發生在哪一層。

為什麼網路上搜到的錯誤訊息和我看到的不一樣?

Docker 各版本的錯誤訊息會改寫。例如舊版的 Cannot connect to the Docker daemon at unix:///var/run/docker.sock 在 Docker 29 變成 failed to connect to the docker API at unix:///var/run/docker.sock。用最後的關鍵字搜尋比較容易找到答案。

docker run 失敗後,為什麼再執行一次會出現名稱已被使用?

很多錯誤(例如連接埠被佔用)發生時,容器其實已經建立了,只是沒有啟動成功。用 docker ps -a 看得到它,處於 Created 狀態。先 docker rm 刪除它再重新執行。

怎麼看容器為什麼退出?

依序執行 docker ps -a 看 STATUS 中的結束代碼、docker logs 容器名稱 看程式輸出,以及 docker inspect -f "{{.State.ExitCode}} {{.State.OOMKilled}}" 容器名稱。結束代碼 137 表示被強制終止(SIGKILL 或記憶體不足),1 通常是程式本身出錯。

更新於 2026年10月13日