How to fix ADD failed: file not found in build context or excluded by .dockerignore in Docker
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.
Quick fix
Fixes ADD failed: file not found in build context or excluded by .dockerignore
docker build -t myapp .- Verify the file exists in the build context directory
- Check your .dockerignore file
- Specify the correct build context in docker build
- Fix the ADD/COPY path in your Dockerfile
docker build -t myapp .ADD failed: file not found in build context or excluded by .dockerignore
What this error means
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.
Typical symptoms
- Docker build fails with "ADD failed: file not found in build context or excluded by .dockerignore"
- Build works locally but fails in CI/CD pipelines with different working directories
- Error message includes "stat [filename]: file does not exist"
- Build succeeds when files are moved into the Dockerfile directory
Common causes
- The file path in ADD/COPY is outside the build context directory
- The file is excluded by patterns in .dockerignore
- Incorrect build context specified in docker build command (using "." when files are elsewhere)
- Using relative paths like "../file" which go outside the build context
- The Dockerfile location differs from the build context, causing path confusion
- Trailing comments in COPY/ADD instructions are interpreted as part of the filename
- File exists locally but is in .gitignore and missing from the repository in CI
How to fix "ADD failed: file not found in build context or excluded by .dockerignore"
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.
Advanced notes
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.
Frequently asked questions
- What does "ADD failed: file not found in build context or excluded by .dockerigno" mean?
- 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.
- How do I fix "ADD failed: file not found in build context or excluded by .dockerigno"?
- 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.
- What causes "ADD failed: file not found in build context or excluded by .dockerigno"?
- The file path in ADD/COPY is outside the build context directory The file is excluded by patterns in .dockerignore Incorrect build context specified in docker build command (using "." when files are elsewhere)
- Is the "ADD failed: file not found in build context or excluded by .dockerigno" error serious?
- It is a low-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚Docker Official Documentation
- 📖Docker Build Context Documentation(Official Docs)
- 💬Stack Overflow: ADD failed file not found(Stack Overflow)
- 💬Stack Overflow: Include files outside build context(Stack Overflow)
- 💬Docker Forums: COPY failed discussion(Stack Overflow)
- 📝