.dockerignore
A file in the build context directory that lists files and directories Docker should exclude when sending the build context to the daemon. Excluding node_modules, .git, and test fixtures reduces build context from gigabytes to megabytes.
.dockerignore — Keeping Your Build Context Fast and Safe
What Is .dockerignore in Simple Terms?
When you run docker build ., Docker sends your entire project directory to the Docker daemon before executing a single Dockerfile instruction. Without .dockerignore, that includes your 500MB node_modules folder, your .git history, your .env secrets file, and every test fixture and build artifact you have ever created. All of it gets sent over the network before the build even starts.
.dockerignore tells Docker which files to exclude. It uses the same syntax as .gitignore.
Without .dockerignore: Sending build context to Docker daemon 890.5MB (890MB sent before build starts) With .dockerignore: Sending build context to Docker daemon 2.3MB (386x reduction — build starts immediately)Production .dockerignore Template
# .dockerignore # Dependencies — always install inside container, never copy from hostnode_modules/npm-debug.log.npm/.yarn/__pycache__/*.pycvenv/.venv/ # Build output — built inside container via multi-stagedist/build/.next/out/target/ # Version control.git/.gitignore.gitattributes # Environment and secrets — NEVER in images.env.env.*!.env.example*.pem*.key*.crt # Development tools.vscode/.idea/*.swp*.swo # Testscoverage/.nyc_output/__tests__/*.test.ts*.spec.ts*.test.js*.spec.js # Docker files themselvesDockerfileDockerfile.*docker-compose.ymldocker-compose.*.yml # Documentationdocs/*.md!README.md # macOS.DS_Store # Logs*.loglogs/Measuring the Impact
# Check build context size before adding .dockerignoredocker build .# Sending build context to Docker daemon 890.5MB # Add .dockerignore, rebuilddocker build .# Sending build context to Docker daemon 2.3MB # 386x reduction — instant improvement with zero other changesImportant Distinction
.dockerignore controls what is SENT TO THE DAEMONDockerfile COPY controls what goes INTO THE IMAGE You need both: .dockerignore: excludes node_modules from context (build speed) COPY src/ /app/src/: copies only src, not everything (image cleanliness) If a file is in .dockerignore: It is NOT sent to daemon COPY instructions cannot access it It cannot end up in the imageTipCreate
.dockerignoreas the very first file in every new project — before writing any Dockerfile. It is much easier to add it at the start than to discover 6 months later that your API keys are in every image you have ever pushed to your registry.
Common MistakeNot adding
.envto.dockerignore. Your.envfile contains real credentials. If it gets sent to the daemon and aCOPY . .instruction runs, it ends up baked into an image layer permanently — visible to anyone who runsdocker historyon the image.
Frequently Asked Questions
Does .dockerignore actually speed up the build, or just keep the image smaller?
Both, but the build-context effect is the one people miss. Before Docker even runs the first instruction, it tars up the entire build directory and streams it to the daemon. If node_modules or a .git history sits in there uncached, that transfer alone can add tens of seconds before the build logic starts, especially over a remote Docker context or in CI where the daemon isn't local.
What's a common mistake teams make with .dockerignore?
Writing it once and forgetting it as the project grows — new build artifacts (dist/, coverage/, .next/) get added over time and nobody updates the ignore file, so context size creeps back up. Also worth knowing: .dockerignore only excludes files from being sent to the daemon, it doesn't affect files already COPYed by an earlier layer, so exclude patterns need to match your actual COPY instructions.