a .dockerignore file Explained

What a .dockerignore file does, with an example.

Published November 9, 2026

A .dockerignore file lists files and folders to exclude from the build context, making builds faster and images smaller and keeping secrets out.

How it works

It works like .gitignore. Excluding node_modules, .git and local env files prevents them from being sent to the Docker daemon and copied by COPY . .

Patterns are relative to the context root.

Try it

cat .dockerignore
node_modules
npm-debug.log
.git
.env
dist

Practical examples

Typical Node project

node_modules
.git
.env
*.log
dist
Dockerfile
.dockerignore

Keep one file with an exception

*.md
!README.md

Check context size

docker build --progress=plain . 2>&1 | grep 'transferring context'

PHP/Laravel

vendor
storage/logs
.env
.git

Common mistakes

  • Copying .env into images.
  • Sending node_modules to the daemon.
  • Forgetting that ignored files can't be COPYed.

Key takeaways

  • A .dockerignore file lists files and folders to exclude from the build context, making builds faster and images smaller and keeping secrets out.
  • Run docker <command> --help for all options.
  • Clean up unused resources regularly.

FAQ

Why is my build slow?

A huge build context; add a .dockerignore.

Where can I see all options?

Run the command with --help.

More General articles