Skip to main content
⚡ OpenDevDocs
Docker
devopsdockerGuideBeginner

The .dockerignore File & Build Optimization

Complete guide to using .dockerignore to exclude unnecessary files from the build context, speed up builds, and protect secrets.

Verified: 2026-09-30
Tested on:docker 27.x
Platforms:
linuxmacoswindows
#docker#dockerignore#optimization#security#devops

The .dockerignore file tells the Docker CLI which files and directories to exclude when packing the Build Context to send to the Docker daemon.


Why .dockerignore is Critical

  1. Faster Build Times: Prevents transferring gigabytes of local files (like node_modules or .git) across the Docker daemon socket.
  2. Deterministic Builds: Ensures local platform binaries (node_modules built on macOS) do not leak into Linux containers.
  3. Security: Prevents accidental inclusion of .env files, SSH keys, credentials, and local logs into image layers.

# Git & version control
.git
.gitignore

# Dependencies
node_modules
npm-debug.log
yarn-error.log
pnpm-lock.yaml

# Build outputs & caches
dist
build
.next
.turbo
coverage
.cache

# Environment variables & secrets
.env
.env.*
!.env.example
*.pem
*.key
id_rsa

# IDE & OS files
.vscode
.idea
.DS_Store
Thumbs.db

# Docker files
Dockerfile*
docker-compose*.yml
.dockerignore
README.md

Pattern Matching Rules

  • Line starting with # is a comment.
  • Leading / matches paths relative to root context.
  • Wildcards * match zero or more characters.
  • ** matches any number of directories recursively.
  • ! exception prefix overrides previously matched patterns.

On this page