Skip to content
Latchkey

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.

docker build output
ERROR: failed to solve: failed to compute cache key: "/app/.env.production": not found
# .dockerignore contains a broad "*.env*" or ".env*" that drops .env.production

Common 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.

.dockerignore
grep -nE 'env|config|build' .dockerignore
# re-include after the broad exclude:
# *.env*
# !.env.production

Narrow the pattern

  1. Replace broad wildcards with specific paths.
  2. Order negation (!) rules after the excludes they override.
  3. Verify the file is in the context by inspecting a built image or using a debug COPY.

How to prevent it

  • Keep .dockerignore patterns specific, not broad wildcards.
  • Place !-negation re-includes after the matching exclude.
  • Review .dockerignore when adding files the Dockerfile must copy.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →