Skip to content
Latchkey

Docker "COPY failed: file not found in build context" in CI

COPY can only see files inside the build context that .dockerignore has not excluded. A missing or ignored source path produces this exact error before the layer is built.

What this error means

A build fails on a COPY line with COPY failed: file not found in build context or excluded by .dockerignore: stat <path>: file does not exist.

docker
Step 5/12 : COPY dist/ /app/dist/
COPY failed: file not found in build context or excluded by .dockerignore: stat app/dist: file does not exist

Common causes

Artifact not built before the COPY

A dist/ or build output directory does not exist yet because the build step that produces it runs outside the image or after it.

Path excluded by .dockerignore

A broad ignore pattern removed the directory from the context.

Wrong relative path

The COPY source is relative to the context root, but the file lives elsewhere.

How to fix it

Ensure the source exists in the context

  1. Build or generate the artifact before docker build, or produce it in an earlier build stage.
  2. Verify the path with ls before building.
Terminal
npm run build   # produces ./dist
docker build -t app .

Adjust .dockerignore

  1. Remove or negate patterns that exclude files COPY needs.
.dockerignore
# .dockerignore
*
!dist/
!package.json

How to prevent it

  • Generate any copied artifacts before the build (or in a prior multi-stage stage), and keep .dockerignore tight but consistent with what the Dockerfile copies.

Frequently asked questions

What causes ""COPY failed: ... not found in build context""?
A dist/ or build output directory does not exist yet because the build step that produces it runs outside the image or after it.
How do I fix "COPY failed: ... not found in build context"?
Ensure the source exists in the context

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card