CodeOath
← All posts
Docker65 min total · 19 parts

Docker Fundamentals: Images, Containers, and Writing a Good Dockerfile

Contents — Part 9 of 19: .dockerignore and the Build Context
Part 9 of 19 · ~1 min

.dockerignore and the Build Context

When you run docker build ., Docker doesn't just read the Dockerfile — it sends the entire directory (the build context) to the daemon first, because any COPY/ADD instruction needs access to those files. Without a .dockerignore, that context includes node_modules, .git, build output, log files, and anything else sitting in the directory, which is slow to send and can leak files you never meant to include into a layer.

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

Two consequences worth internalizing: a large, unfiltered context makes every build slower (the daemon has to receive and hash all of it before the first instruction even runs), and a missing .dockerignore is a real way secrets leak — a COPY . . with a .env file sitting in the directory bakes that file's contents into an image layer, where it's recoverable by anyone who can pull or inspect the image, even if a later layer "deletes" it (see the layers section above — deletion in a later layer doesn't remove it from history).