A Dockerfile is a short text file with an outsized effect. It decides how big your image is, how long your builds take, how many known vulnerabilities ship inside it, and whether a container that escapes its process boundary runs as root on the host. A careless Dockerfile can turn a small application into a huge image that takes minutes to pull on every deploy, carries a compiler and a package manager it never needs at runtime, and runs as root by default. A good one does the opposite: it builds fast because the cache works for it, it ships only what the application needs, and it gives an attacker almost nothing to work with.
This guide covers Dockerfile best practices as they stand on 30 November 2026, against Docker Engine 29.10.1, released 24 November 2026, with BuildKit 0.35.1 as the builder. Every practice below is one Docker itself documents or that follows directly from how layers and the build cache work.β¦