Docker in Production
# CHAPTER 16
Docker in Production
1. Introduction
Running Docker on your local laptop to test a PHP script is vastly different from running a highly available e-commerce platform handling 10,000 users a minute. In a production environment, servers crash, memory leaks occur, and applications freeze. You cannot be awake at 3:00 AM to manually typedocker restart. In this chapter, we will architect resilience, exploring production-grade concepts like Restart Policies, Health Checks, and the conceptual leap required for true Container Orchestration.
2. Learning Objectives
By the end of this chapter, you will be able to:- Configure automated Restart Policies to recover from crashes.
- Implement Docker Health Checks to detect frozen applications.
- Understand the limitations of raw Docker Compose in production.
- Define Container Orchestration.
- Prepare a container for a production deployment.
3. Beginner-Friendly Explanation
Imagine hiring an employee (The Container) to monitor a security desk.-
Local Development: The employee falls asleep (The app freezes). You walk over, tap them on the shoulder, and wake them up (You type
docker restart).
- Production (Restart Policies): You install a mechanical chair. If the employee stops typing for 5 minutes, the chair automatically tips them onto the floor, waking them up immediately.
- Production (Health Checks): The employee is awake, but they are wearing a blindfold. They are technically "Running", but they are useless. You install a robot that asks the employee a question every 30 seconds. If the employee gives the wrong answer, the robot fires them and hires a new employee instantly.
4. Restart Policies
By default, if a container crashes due to an error, it stays dead. You can configure the Docker Daemon to automatically resuscitate the container using Restart Policies.Common policies:
-
no: The default. Do not restart.
-
always: Always restart the container if it stops. If you reboot the physical host server, Docker will automatically launch this container on startup.
-
unless-stopped: Always restart the container, *unless* you manually typeddocker stop. (This is the industry standard for production web servers).
*In Docker Compose:*
5. Health Checks
A container's status might say "Up," but the Node.js application inside it might be frozen in an infinite loop, returning500 Errors to users.
A Health Check allows Docker to test the actual *internal health* of the application.
You tell Docker: "Every 30 seconds, send an HTTP request to localhost/api/health. If it fails 3 times in a row, mark the container as UNHEALTHY."
*In Docker Compose:*
6. The Limitation of Docker Compose
Docker Compose is incredible for single-server deployments. If you rent a $5 DigitalOcean VPS, install Docker, and rundocker-compose up -d, you have a great production server.
The Problem: What if that single VPS physically catches fire? The container, the data, and your business are gone.
Raw Docker Compose cannot manage containers across *multiple* physical servers. To achieve true High Availability, you must graduate to Container Orchestration.
7. Introduction to Container Orchestration
When you have 5 physical servers (Nodes) and 50 containers, you need a "Manager" (An Orchestrator). An Orchestrator solves massive production problems:- Scheduling: It looks at the 5 servers, finds the one with the most free RAM, and launches the container there.
- Self-Healing: If Server A catches fire, the Orchestrator instantly notices the 10 containers on it died, and automatically recreates them on Servers B, C, and D.
- Scaling: If traffic spikes, it commands: "Scale the web container from 2 replicas to 20 replicas."
The two most famous Orchestrators are Docker Swarm (built into Docker) and Kubernetes (the global industry standard).
8. Mini Project: A Production-Ready docker-compose.yml
Let's build a resilient configuration.
Step-by-Step Tutorial: Review this production-grade Compose file:
This architecture is self-healing, data-persistent, strictly versioned, and secured against leaked secrets.
9. Best Practices
-
Graceful Shutdowns: When you update an image, Docker sends a
SIGTERMsignal to the old container, asking it to stop politely. If your application code ignores this signal, Docker waits 10 seconds and then forcefully kills (SIGKILL) the container, potentially dropping active user database transactions! Always program your applications to handle shutdown signals gracefully.
10. Common Mistakes
- Ignoring Log Management: As discussed in Chapter 13, running containers in production without a log rotation limit will eventually fill the host server's hard drive to 100%, causing a catastrophic total system crash.
11. Exercises
- 1. Contrast a container crashing (the process stopping) with a container becoming "Unhealthy". Why are Health Checks necessary if Restart Policies already exist?
-
2.
Why is raw
docker-composeinsufficient for an enterprise architecture requiring High Availability across multiple physical data centers?
12. FAQs
Q: Can I usedocker-compose in production at all?
A: Yes! For a small blog, a personal portfolio, or an internal company tool used by 10 people, running docker-compose up on a single AWS EC2 instance is perfectly acceptable and highly cost-effective. You only need Orchestration (Kubernetes) when downtime is financially unacceptable.
13. Interview Questions
-
Q: Explain the mechanical difference between the
alwaysandunless-stoppedDocker restart policies. In a production web server environment, which policy is generally preferred and why?
- Q: A Node.js API container is technically in the "Up" state, but it is entirely unresponsive to HTTP requests due to a deadlocked thread. Describe the Docker feature you would implement to automatically detect and remediate this frozen state.