Docker — Best Practices and Pro Tips for Writing Dockerfiles

Ashish Patel
DevOps Mojo
Published in
7 min readOct 21, 2023

--

Production Pro Tips: Overview of best practices for writing Dockerfile.

DevOps Mojo — Dockerfiles Best Practices and Tips

Below is the list of recommended best practices and methods for building efficient images.

Build the smallest image possible

Building a smaller image offers advantages such as faster upload and download times. This is important for the cold start time of a pod in Kubernetes: the smaller the image, the faster the node can download it.

Use Multi-stage Dockerfiles

Multi-stage builds feature allows you to use multiple temporary images during the build process, but keep only the latest image as the final artifact. In other words, exclude the build dependencies from the image, while still having them available while building the image. Multi-stage builds let you reduce the size of your final image, by creating a cleaner separation between the building of your image and the final output. Less dependencies and reduced image size.

For example, for a .NET/Java applications running, use one stage to do the compile and build, and another stage to copy the binary artifact(s) and dependencies into the image, discarding all nonessential artifacts.
Another example is, for an Angular/React applications, run the npm…

--

--

Ashish Patel
DevOps Mojo

Cloud Architect • 4x AWS Certified • 6x Azure Certified • 1x Kubernetes Certified • MCP • .NET • Terraform • DevOps • Blogger [https://bit.ly/iamashishpatel]