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
COPYinstructions β runls <path>from the context root to confirm. - Keep your
.dockerignorefile reviewed and up to date β itβs easy to accidentally exclude files you need (like.env.exampleor config files).
Related: Docker cheat sheet Β· Docker complete guide Β· Docker vs Kubernetes