Tool brief

Docker Best Practices: Builds, Compose and Recovery

Build smaller reproducible images, separate service health from readiness, and rehearse update and volume recovery before relying on Docker in production.

Editorial update

Decision in brief

Build and tag an image once in CI, then promote that immutable artifact. Use Docker Compose when a single host meets your recovery and availability targets. Keep restore and rollback procedures alongside the Compose definition, and evaluate a multi-host platform when an outage of that host would breach your targets.

Promote one image: Build once. Tag the image. Promote the tag.
Promote the same immutable image between environments. Keep rollback and restore procedures alongside the deployment definition.

Choose a container workflow that fits deployment and recovery

Use a multi-stage Dockerfile when build tools and artifacts can be separated from runtime. Copy dependency manifests before frequently changed source to reuse stable dependency layers. Add a .dockerignore that excludes credentials, local databases and build outputs from the context. Record the base digest in source and update it through a reviewed dependency change. A digest gives reproducibility and requires an update process to keep receiving patches.

Keep runtime configuration in Compose or your deployment platform and persist state in a named volume or a managed service. Compose can wait for a dependency's health check if configured for that condition. Give the application its own readiness check and retry behavior, since a dependency can fail after startup.

WorkloadFirst approach to tryCheck before adoption
Stateless service with frequent changesMulti-stage image built in CI and promoted by digestTime builds after a source-only change and verify the same digest in staging and production promotion.
Few services on a replaceable Linux hostCompose, explicit health checks and a documented restore procedureProve that a fresh host can serve healthy traffic and recover the required application data.
Service with strict host-outage or traffic-interruption targetsCompare a managed or multi-host runtime against the simplest working deploymentMeasure endpoint error rate during a deploy and recovery after losing a host.
Service with persistent dataNamed volume or managed storage and recoverable backupsRestore a representative dataset and verify records before accepting writes.

What two HN operators put under review

In the smaller-images thread, grim_io reported encountering multi-gigabyte images and teams unfamiliar with multi-stage builds. A useful build comparison records runtime image size, cold pull time and warm build time, with the same application and integration suite.

In the Compose deployment thread, hnlmorg pointed out that the cited workload could run on one host and identified loss of that host as a reason to evaluate a different deployment architecture. Rehearse restart, rollout and whole-host replacement as separate scenarios.

Run a build, rollout and restore rehearsal

Use one staging application with synthetic state. Agree on cold pull time, deploy error budget, maximum recovery time and acceptable data age before comparing changes.

  1. Build from a clean checkout and then change only application source. Record each build time, image size and digest. Run the same functional tests against both the existing and proposed runtime images.
  2. Exercise a staged rollout under steady test traffic and record error rate and readiness time. Roll traffic back to the previous digest and confirm it can read the current schema and seeded records.
  3. Recreate the environment on a disposable host and restore the latest staged backup. Check record counts and application reads, then record elapsed time and data age.
  4. Promote only if build reproducibility, the deployment error budget and both recovery objectives pass. Pause a migration that makes the previous app or restored data unusable until the rollback procedure covers it.

Related articles