Docker Ignore

Posted by Rico's Nerd Cluster on June 7, 2024

A .dockerignore file lives next to your Dockerfile and controls which files are excluded from the Docker build context.

  • Why use .dockerignore?
    • ✅ Faster builds: smaller context → quicker transfer to the Docker daemon.
    • ✅ Smaller images: avoid accidentally copying huge or irrelevant files.
    • ✅ Better security: prevents secrets, credentials, .git/, etc. from leaking into images.

Example Project Layout

1
2
3
4
5
6
├── .dockerignore
├── Dockerfile
├── app/
│   └── ...
├── node_modules/
└── secrets.env

.dockerignore:

1
2
3
4
5
6
7
# don't send deps or creds into the image
node_modules
secrets.env

# logs, caches, etc.
*.log

When you run docker build ., Docker will send only:

1
2
Dockerfile
app/      (and its contents)

…not node_modules/, secrets.env, or any .log files.