docker · Docker
報錯原文
COPY failed: file not found in build context or excluded by .dockerignore
這是什麼意思
COPY 只能從建置上下文裡取檔案,而那個檔案不在其中。上下文是 docker build 的最後一個參數,所以通常是你用 ../x 指向了上一層目錄,或者 .dockerignore 把那個檔案濾掉了——忽略了 node_modules 或 *.env,卻又要 COPY 其中的檔案,是最典型的。這條訊息同時點出兩種原因,所以先 cat .dockerignore,再按上下文根目錄重寫路徑。靠擴大上下文來解決,意味著整個目錄都要傳給守護行程,每次建置都會變慢。
怎麼修
cat .dockerignore- 來自
- docker
- Docker
- 12
docker 的錯誤要先歸到某一層才讀得懂——用戶端連不上守護行程、映像檔倉庫拒絕你、建置過程中某條 RUN 失敗、容器一起來就死掉,是四種不同的問題——而在建置和執行這兩層,那行字本身並不是原因:原因在裡面那條命令留下的輸出裡,各層修復的代價也不一樣。
怎麼讀報錯
- 從第一行往下讀。越往下越是工具內部的事,起因通常寫在最上面。
- 有檔名和行號就從那裡查——不是堆疊最上面那一格,而是最上面那條提到你自己寫的檔案的行。
- 把報錯原文照樣去搜,但先去掉你自己的路徑和變數名,正是那些讓搜尋比對不上。
- 同一種情況在不同版本裡措辭不同。結果不對頭,就把版本號一起加進查詢。
- 貼上修法之前,先確認它會丟掉什麼。這裡面有些是不能收回的。
常見問題
Q. “COPY failed: file not found in build context or excluded by .dockerignore” 是什麼意思?
COPY 只能從建置上下文裡取檔案,而那個檔案不在其中。上下文是 docker build 的最後一個參數,所以通常是你用 ../x 指向了上一層目錄,或者 .dockerignore 把那個檔案濾掉了——忽略了 node_modules 或 *.env,卻又要 COPY 其中的檔案,是最典型的。這條訊息同時點出兩種原因,所以先 cat .dockerignore,再按上下文根目錄重寫路徑。靠擴大上下文來解決,意味著整個目錄都要傳給守護行程,每次建置都會變慢。
Q. 怎麼修?
cat .dockerignore —— 執行前先看上面的說明,確認這條命令會丟掉什麼。
Q. 這是哪個工具報的?
docker。它屬於Docker,報錯原文有 12 個詞。
相關報錯
failed to solve: process "/bin/sh -c npm ci" did not complete successfully: exit code: 1buildkitstandard_init_linux.go: exec user process caused: no such file or directoryruncexec /usr/local/bin/entrypoint.sh: exec format errordockerOCI runtime create failed: exec: "bash": executable file not found in $PATH: unknownruncCannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?dockerBind for 0.0.0.0:8080 failed: port is already allocateddockerConflict. The container name "/api" is already in use by containerdockerfailed to register layer: Error processing tar file(exit status 1): no space left on devicedocker