Keeping the build context lean: .dockerignore

Docker · Practical · 6 min

Without it, node_modules, .git and .env files end up slowing builds or, worse, inside your image.

The build context is everything in the directory you pass to docker build. A `.dockerignore` file in the root of the context lists patterns to exclude, much like .gitignore.

Why it matters:

  • Speed: sending a 2 GB node_modules or .git directory to the builder wastes time on every build.
  • Cache stability: COPY . . checksums everything it copies. If .git or log files are included, the cache breaks whenever they change, even when your code didn't.
  • Security: .env files, SSH keys and cloud credentials sitting in the project folder get baked into the image by COPY . ., and anyone who pulls the image can read them.
  • Correctness: copying your host's node_modules (built for macOS) over the container's Linux-built ones causes confusing native-module errors.

Syntax essentials:

  • One pattern per line; # starts a comment.
  • Paths are relative to the context root; * matches within a path segment, ** across any number of directories.
  • !pattern re-includes something excluded earlier, and the last matching line wins.
  • Some teams invert it: start with *, then allow only what's needed with !src and !package*.json.

The file must sit at the root of the build context, not necessarily next to the Dockerfile. With BuildKit you can also give a Dockerfile its own ignore file by naming it after it, for example Dockerfile.prod.dockerignore.

Example

# .dockerignore
.git
.gitignore
**/node_modules
**/__pycache__
*.log
dist/
coverage/

# secrets and local config
.env
.env.*
!.env.example
**/*.pem

# not needed inside the image
Dockerfile*
compose*.yaml
README.md

Remember

  • .dockerignore excludes files from the build context
  • It speeds up builds, stabilises the cache and keeps secrets out
  • It must be at the context root; ! re-includes; last match wins

Watch out for

  • Relying on .gitignore — Docker ignores it; only .dockerignore affects the build context.

Check yourself

  1. Your build cache keeps busting on COPY . . even though source files didn't change. What is a likely cause?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.docker.com/build/concepts/context/#dockerignore-files