Running containers in production
Restart policy, health checks, signals and when to move to an orchestrator.
A checklist
- Tag versions explicitly (
myapp:1.4.2), never deploylatest. - Restart policy:
--restart unless-stoppedbrings 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 --interval=30s --timeout=3s --retries=3 \
CMD wget -qO- http://localhost:3000/health || exit 1Graceful 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.