Chapter 1 · Practice 4 of 5
Add a .dockerignore file to shrink the build context
Why it matters
Everything in the build context is sent to the builder, and COPY . . copies all of it into the image. Without a .dockerignore, that includes:
node_modulesand build output such asdist- the
.gitfolder and test fixtures - worst of all, local
.envfiles with real secrets
The .dockerignore file uses syntax similar to .gitignore. Add it in the same commit as the Dockerfile so the two never drift apart.
To check it, look at the "transferring context" size in the build output: it should be a few megabytes for most apps, not hundreds.
Example
.git
node_modules
dist
coverage
.env
.env.*
*.log
Dockerfile
compose.yaml