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.