brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a .dockerignore file makes builds faster and images smaller

Docker
~/tips/tip-docker-dockerignore

.git

Keeps unneeded and sensitive files out of the build context so builds run faster.

Every docker build starts by sending the build context to the builder. If your project folder has a big node_modules or a .git history, all of that goes along.

A .dockerignore file next to the Dockerfile keeps files out of the context. The syntax is close to .gitignore.

.git
node_modules
*.log
tmp*
.env

Besides speed, there is a second benefit. A COPY . . step no longer picks up your local .env or other secrets by accident, and a changed log file no longer invalidates the layer cache for that step.

Anchor a pattern with a leading path if you need to, and use ! to re-include something, in the same way as in a gitignore file. Put the file in the root of the build context, which is the folder you pass to the build command.

The caveat: the rules apply to the whole context, so you cannot ignore a file for one COPY and include it for another. The docs call the mechanism coarse. It suits temp files, logs and build output. If an ignored file is needed in the build, it will be missing and the error will point at the COPY line.

Further reading: Optimize cache usage in builds, On this site: RUN –mount=type=cache keeps pip and apt downloads between builds, On this site: docker image prune -a –filter until= only removes old images

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link