This error occurs when Docker's ADD or COPY instruction tries to access a file that either doesn't exist in the build context directory, is located outside the context, or is excluded by .dockerignore.
This error appears during a Docker image build when the ADD (or COPY) instruction in your Dockerfile references a file that the Docker daemon cannot find or access. The build context is the set of files located at the path specified in your docker build command—only files within this context can be added to the image. Docker sends the entire build context to the daemon at the start of the build process. If your ADD instruction references a file outside this context, in a parent directory, or excluded by .dockerignore patterns, the build will fail with this error. Understanding the build context is crucial: it's not just about where your Dockerfile is located, but where you run the docker build command from and what path you specify as the context argument.
First, confirm that the file you're trying to ADD actually exists within your build context. If you're running:
docker build -t myapp .Then the build context is the current directory (.). List all files to verify:
ls -laIf the file is in a parent directory or elsewhere, you have two options:
1. Move the file into the build context
2. Change the build context to include the file (see next steps)
The .dockerignore file (if it exists) may be excluding your file. Check for patterns that match your file:
cat .dockerignoreCommon problematic patterns:
- * (excludes everything)
- *.log (might match your file extension)
- Directory patterns that include your file
To fix, either:
1. Remove the pattern excluding your file
2. Add an exception using ! before the pattern:
# Exclude all logs
*.log
# But include this specific one
!important.logNote: The last matching line wins, so order matters!
If your file is outside the current directory, change the build context. The syntax is:
docker build -t imageName -f /path/to/Dockerfile /path/to/contextExample: If your Dockerfile is in ./docker/ but needs to COPY files from the project root:
# Instead of this (fails):
docker build -t myapp ./docker
# Use this (context is parent directory):
docker build -t myapp -f ./docker/Dockerfile .For docker-compose, adjust the context in your docker-compose.yml:
services:
app:
build:
context: ../.. # Go up two directories
dockerfile: ./project/docker/DockerfileEnsure your ADD/COPY paths are relative to the build context, not the Dockerfile location. You cannot use ../ to go outside the context:
# ❌ WRONG - tries to go outside context
ADD ../config.json /app/
# ✅ CORRECT - relative to context root
ADD config.json /app/
# ✅ CORRECT - file in subdirectory of context
ADD configs/app.json /app/Also check for trailing comments (they're not ignored!):
# ❌ WRONG - Docker looks for "file.txt # comment"
COPY file.txt /app/ # important file
# ✅ CORRECT
# important file
COPY file.txt /app/Ensure Docker can read the file:
# Check file permissions
ls -l yourfile.txt
# Make readable if needed
chmod 644 yourfile.txtWatch for case sensitivity issues, especially when building on case-insensitive filesystems (macOS/Windows) that deploy to Linux:
# If your file is "Config.json" but you write:
COPY config.json /app/ # This might fail on Linux!Always match the exact case.
Build Context Best Practices:
- Keep your build context small—Docker sends the entire context to the daemon, which can be slow for large directories
- Use .dockerignore aggressively to exclude node_modules, .git, and other large directories
- Consider using multi-stage builds to separate build-time and runtime dependencies
Alternative: Using named build contexts (Docker 20.10+):
For complex projects, you can reference files from multiple contexts:
docker build --build-context otherproject=../other/path -t myapp .Then in your Dockerfile:
FROM alpine
COPY --from=otherproject src/file.txt /app/CI/CD Considerations:
This error often appears in CI pipelines where the directory structure differs from local development. Always use explicit build context paths in CI scripts, and verify files are committed to git (not just present locally).
Docker Compose Context Gotcha:
In docker-compose.yml, the context path is relative to the compose file location, not your current directory when running docker-compose up.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec