Git β€’ DevOps & Cloud

Docker (.dockerignore) Template

When running `docker build`, the entire directory is sent as build context to the Docker daemon. A proper `.dockerignore` file prevents sending gigabytes of local node_modules or secret credentials into your Docker image layers.

.dockerignore(31 lines)
1# Version control
2.git
3.gitignore
4
5# Dependencies (install cleanly inside Dockerfile)
6node_modules
7npm-debug.log
8
9# Secret and local environment files
10.env
11.env.*
12!.env.example
13
14# Build and test outputs
15dist
16build
17coverage
18.next
19.nuxt
20
21# Docker files
22Dockerfile*
23docker-compose*
24.dockerignore
25
26# IDEs and OS files
27.vscode
28.idea
29.DS_Store
30*.log
31README.md

Key Rules & Why They Are Ignored

.git

Drastically reduces build context transfer time and prevents exposing commit history inside production images.

node_modules

Forces `npm ci` to run inside the container for architecture-matching native binary builds (e.g. Linux x86/ARM).

.env*

Prevents accidentally baking production passwords directly into immutable Docker image layers.

Already Committed Ignored Files? Purge from Git Cache

Adding a pattern to .gitignore does NOT delete files that were already tracked in previous commits. Run this command to remove them from tracking without deleting your local copies:

git rm -r --cached . && git add . && git commit -m "Untrack ignored files"

Frequently Asked Questions About Docker (.dockerignore) .gitignore

Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

Docker will transfer your entire project directory (including node_modules and .git) over the socket to the build daemon, causing sluggish builds and potentially leaking credentials into public image registries.