failed to connect to the docker API

已經寫好 無法執行 Dockerfile, 執行 docker build 後顯示錯誤訊息:

PS C:\max-new-app\my-ai-app> docker build -t max-new-app .
ERROR: failed to connect to the docker API at npipe:////./pipe/dockerDesktopLinuxEngine; check if the path is correct and if the daemon is running: open //./pipe/dockerDesktopLinuxEngine: The system cannot find the file specified.

解法

這個錯誤訊息表示你的 Docker 指令找不到 Docker 引擎,通常是因為 Docker Desktop 尚未啟動,或者 背景服務沒有正常運行

請依照以下步驟檢查並排除問題:

1. 啟動 Docker Desktop

最常見的原因是你還沒打開 Docker Desktop。請在 Windows 搜尋列輸入 Docker Desktop 並執行它。

  • 觀察右下角系統匣(System Tray)的小鯨魚圖示。
  • 必須等到小鯨魚圖示停止閃爍,且顯示 Docker Desktop is running 綠色狀態,才能執行 docker build

2. 檢查 WSL 2 整合設定

如果你在 PowerShell 執行指令,Docker 需要與 WSL 2 正確連動。請開啟 Docker Desktop 視窗進行檢查:

  1. 點擊右上角的 Settings (齒輪圖示)
  2. 進入 General,確認 Use the WSL 2 based engine 有被勾選。
  3. 進入 Resources > WSL integration,確認 Enable integration with my default WSL distro 有開啟。

3. 重啟 Docker 服務

如果 Docker Desktop 看起來有開但還是報錯,請嘗試重啟:

  1. 右鍵點擊系統匣的鯨魚圖示。
  2. 選擇 Restart
  3. 如果還是不行,可以嘗試在管理員權限的 PowerShell 執行以下指令來強制重啟服務:

PowerShell

net stop com.docker.service
net start com.docker.service

4. 權限檢查

有時候權限不足也會導致無法存取 npipe(具名管道)。請試著關閉目前的 PowerShell,改用 以管理員身分執行 再次進入目錄執行:

PowerShell

cd C:\max-new-app\my-ai-app
docker build -t max-new-app .

成功執行後的下一步

docker build 成功後,你會看到一系列的 Step 1/X 跑完。接著請執行:

PowerShell

# 啟動並對應埠號
docker run -d -p 8080:80 --name policy-web max-new-app

然後在瀏覽器輸入 http://localhost:8080 即可驗證。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *