← All posts

Docker: from "works on my machine" to an image

27 August 2024 · 1 min read
#docker#containers

Every developer has said "it works on my machine". Containers are the fix: you ship the machine.

What I keep in mind when writing a Dockerfile:

  • Small base image. A slim image means fewer packages, faster pulls and fewer security issues.
  • Dependencies before code. Copy the dependency list and install it first, then copy the code. Docker caches the slow step, so rebuilds take seconds.
  • Don't run as root. Create a normal user and switch to it.
  • No secrets in the image. Anything baked in can be pulled out by anyone who has the image. Pass secrets in at runtime.
  • Add a health check. It tells the platform whether the app is actually working, not just whether the process is running.

Once the image is right, it runs the same way on my laptop, in CI and in production.

T
Written by Teza Wynn Htut

Cloud and infrastructure engineer in Auckland. I write down the fixes and lessons from things I build, and make tutorial videos on YouTube.

Watch on YouTube