COPY failed: file not found in build context
Dockerビルドでファイルが見つからない場合のエラー
概要
COPY failed: file not found in build context は、Dockerビルド時にCOPY命令で指定したファイルがビルドコンテキスト内に見つからない場合に発生するエラーです。
エラーメッセージ
COPY failed: file not found in build context or excluded by .dockerignore: stat app.js: file does not exist
COPY failed: stat /var/lib/docker/tmp/docker-builder123/src: no such file or directory
原因
1. ファイルパスが間違っている
| |
2. ビルドコンテキストの外にファイルがある
| |
3. .dockerignoreで除外されている
# .dockerignore
node_modules
*.log
src/ # ← これによりsrc/以下がすべて除外される
4. ビルドコンテキストの指定が間違っている
| |
解決策
1. 正しいパスを指定
| |
2. ビルドコンテキストを正しく指定
| |
3. .dockerignoreを確認
# .dockerignore
# NG: src全体を除外
# src/
# OK: 特定のファイルのみ除外
src/*.log
src/test/
node_modules/
.git/
4. 絶対パスを使用しない
| |
ビルドコンテキストの確認
| |
よくあるパターン
マルチステージビルド
| |
条件付きCOPY
| |
.dockerignoreのデバッグ
| |
docker-compose での注意
| |
| |
ADD vs COPY
| |
関連エラー
関連エラー
Docker の他のエラー
この記事は役に立ちましたか?