πŸ”§ Error Fixes
Β· 1 min read

Docker: COPY Failed β€” File Not Found in Build Context


COPY failed: file not found in build context

Docker can’t find the file you’re trying to COPY. The build context is the directory you pass to docker build.

Why this happens

When you run docker build, Docker sends the entire build context directory to the Docker daemon. The COPY and ADD instructions can only access files within this context β€” they cannot reach files outside it or files excluded by .dockerignore. The error means the path you specified in your Dockerfile doesn’t match any file in the build context that was sent to the daemon.

Fix 1: Check your build context

# The . at the end is the build context
docker build -t myapp .

Files must be inside this directory. Docker can’t access files outside it.

Fix 2: Check .dockerignore

Your .dockerignore might be excluding the file:

cat .dockerignore

Fix 3: Check the path in Dockerfile

# ❌ Wrong path
COPY src/app.js /app/

# βœ… Make sure the file exists relative to build context
# Run: ls src/app.js
COPY ./src/app.js /app/

Alternative solutions

If you need files from outside the build context, restructure your build to use a parent directory as context:

# Use parent directory as context, specify Dockerfile location
docker build -t myapp -f ./app/Dockerfile .

You can also use multi-stage builds to copy artifacts between stages without needing them in the build context at all.

Prevention

  • Always verify file paths relative to the build context directory before adding COPY instructions β€” run ls <path> from the context root to confirm.
  • Keep your .dockerignore file reviewed and up to date β€” it’s easy to accidentally exclude files you need (like .env.example or config files).

Related: Docker cheat sheet Β· Docker complete guide Β· Docker vs Kubernetes

πŸ“˜