Docker .dockerignore Silently Excludes a Needed File in CI
A .dockerignore pattern is broader than intended and removes a file the Dockerfile copies. The build context never includes it, so a later COPY fails or the image is built without it.
What this error means
A build fails with "not found" / "no source files" for a file that clearly exists in the repo - or the image builds but is missing a file at runtime. The file is present on disk but excluded from the context by .dockerignore.
ERROR: failed to solve: failed to compute cache key: "/app/.env.production": not found
# .dockerignore contains a broad "*.env*" or ".env*" that drops .env.productionCommon causes
An over-broad ignore pattern
A wildcard like *, .env*, or config* matches more than intended and removes a file the build relies on.
A negation (!) rule in the wrong order
.dockerignore evaluates patterns in order; a re-include (!keep.me) placed before a broader exclude is overridden, so the file stays excluded.
A directory excluded along with a needed child
Ignoring a whole directory (build/) also drops a file inside it the Dockerfile copies.
How to fix it
Check what the ignore file matches
Grep the pattern and re-include the needed file with a negation after the exclude.
grep -nE 'env|config|build' .dockerignore
# re-include after the broad exclude:
# *.env*
# !.env.productionNarrow the pattern
- Replace broad wildcards with specific paths.
- Order negation (
!) rules after the excludes they override. - Verify the file is in the context by inspecting a built image or using a debug COPY.
How to prevent it
- Keep
.dockerignorepatterns specific, not broad wildcards. - Place
!-negation re-includes after the matching exclude. - Review
.dockerignorewhen adding files the Dockerfile must copy.