docker build COPY failed — no such file or directory
Quick Answer
# The build context is the directory you pass to docker build
# Files must exist within the build context
# Run build from the directory containing your files
docker build -t my-image .
# Check what is in the build context (before Docker sees it)
# List files that would be included
cat .dockerignore
When this happens
COPY failed: file not found in build context or excluded by .dockerignore:
stat app/package.json: file does not exist
Docker's COPY instruction can only reference files inside the build context. If the file is outside the context path, or listed in .dockerignore, it will not be visible.
Other causes & fixes
Check .dockerignore is not too aggressive
# .dockerignore
node_modules
dist
.git
# Make sure you are not ignoring files you need to COPY
Widen the build context
Pass a parent directory as the context and specify the Dockerfile location with -f:
# Build context is parent dir; Dockerfile is in ./app/
docker build -f app/Dockerfile -t my-image ..
Correct COPY paths
# Copies ./src from the host build context to /app/src in the image
COPY src/ /app/src/
# NOT this — path must be relative to build context, not your machine
# COPY /home/user/project/src/ /app/src/ ← WRONG
Confirm the file is inside the build context
The final path in a COPY instruction is resolved from the build context, not from the directory containing the Dockerfile. Check both paths before changing the Dockerfile.
pwd
find . -maxdepth 3 -type f -name 'package.json' -o -name 'src'
# Dockerfile in ./app, context is the parent directory
docker build -f app/Dockerfile -t my-image ..
Check .dockerignore and file name casing
A file can exist on the host but still be excluded by .dockerignore. Linux containers also treat App and app as different paths.
sed -n '1,200p' .dockerignore
ls -la path/to/source
# Keep COPY paths relative to the context
COPY package.json /app/package.json
Related