appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Running containers in production

Lesson 4 of 410 minAdvanced

Restart policy, health checks, signals and when to move to an orchestrator.

A checklist

  • Tag versions explicitly (myapp:1.4.2), never deploy latest.
  • Restart policy: --restart unless-stopped brings containers back after a crash or a reboot.
  • Health check: let Docker know when the app is really ready.
  • Limits: memory and CPU limits for every container.
  • Non-root user, read-only filesystem, dropped capabilities (see the security lesson).
  • Logs to stdout with size limits, shipped to a central place.
  • Backups: volumes are not backed up for you. Test a restore.
HEALTHCHECK in a Dockerfile
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
  CMD wget -qO- http://localhost:3000/health || exit 1

Graceful shutdown

docker stop sends SIGTERM and, after 10 seconds, SIGKILL. Your application must handle SIGTERM: finish current requests and close connections. In a Dockerfile, use the exec form CMD ["node", "server.js"]. The shell form wraps the command in sh -c, which does not forward signals, so your app never receives them.

When one host is not enough

Compose on one server is fine for small services. When you need several machines, automatic rescheduling, rolling updates and service discovery, move to an orchestrator such as Kubernetes (see the Kubernetes course).

CarefulNever keep the only copy of important data inside a container or an unbacked-up volume. Containers are disposable by design.

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

1. Why use the exec form CMD ["node", "server.js"]?
2. What does docker stop send first?
3. Which tag practice is safest for production?

Key terms