Docker Cookbook
“How would you do X?” — practical, production-ready recipes with the approach, full code, pitfalls and variations.
Docker
- Add a healthcheck to an HTTP serviceLet Docker and Compose know when a container is actually ready and when it has become unhealthy.
- Build a layered image for a Spring Boot appPackage a Java service so dependency layers are cached and only your classes change on rebuild.
- Build a production image for a Node.js servicePackage a Node.js or TypeScript service with only production dependencies and a non-root user.
- Build a production image for a Python web appPackage a Python API (FastAPI, Flask, Django) in a small, cache-friendly, non-root image.
- Build, cache and push images in GitHub ActionsBuild images in CI with layer caching and push them with immutable tags.
- Build one image tag for amd64 and arm64Run the same image on x86 servers, ARM servers and Apple Silicon laptops.
- Configure nginx from environment variablesUse one nginx image across environments with different upstream URLs and server names.
- Develop with Compose and live reloadEdit code on the host and see changes in the running container without rebuilding.
- Make containers cope with dependencies that start slowlyThe app crashes when the database isn't ready yet, in Compose and after database restarts.
- Run containers as non-root with a read-only filesystemReduce what a compromised container can do without breaking the app.
- Run PostgreSQL in Compose with a healthcheck and persistent dataStart a database for development that keeps data and that the app waits for.
- Scan images for vulnerabilities in CIFind known CVEs in base images and dependencies before images reach production.
- Serve a single-page app from a small imageBuild a React or Vue app and serve it with correct routing fallback and caching headers.
- Ship a Go service in a tiny distroless imageCompile a static Go binary and run it in an image with no shell or package manager.
- Shrink a bloated imageAn image is over 1 GB; make it smaller without breaking it.
- Speed up builds with BuildKit cache mountsStop re-downloading packages on every build when only one dependency changed.
- Start optional tools only when needed with Compose profilesKeep admin UIs, mail catchers and debug tools out of the default stack but one flag away.
- Stop container logs from filling the diskA chatty container filled the host disk with log files; configure rotation for every container.
- Use private package credentials in a build without leaking themInstall from a private npm, PyPI or Maven registry during the build without the token ending up in the image.
- Write an entrypoint script that handles signalsRun setup steps before the app without breaking graceful shutdown.