06. Container Lifecycle
A container is the runnable instance of a Docker image. It is an isolated, secure, and highly efficient environment where your application actually executes. Understanding how containers live, die, and handle processes is crucial for operating robust applications.
6.1 Container Lifecycle
Section titled “6.1 Container Lifecycle”A container moves through several distinct states during its existence.
Created ↓ Running ↓ Stopped (Exited) ↓ Removed- Created: The container has been defined and allocated by Docker (e.g., via
docker create), but the main application process has not been started yet. - Running: The container’s main process is actively executing on the host CPU.
- Stopped: The main process has terminated (either normally or due to a crash), but the container’s filesystem and configuration still exist on disk. It can be restarted.
- Removed: The container instance, its logs, and its non-persistent writable layer have been permanently deleted from the host machine.
6.2 Container Processes
Section titled “6.2 Container Processes”A container is not an operating system; it is just a wrapper around a Linux process.
Inside every Linux system, Process ID 1 (PID 1) is the initialization process (like systemd) that manages all other processes.
In a Docker container, your application is PID 1. When PID 1 stops, the container dies.
Main Process
Section titled “Main Process”Because your application is the main process, a container should ideally only run one main thing (e.g., just the web server, or just the database). If you need an Nginx web server and a PostgreSQL database, you should run two separate containers, not cram both into one.
Signals
Section titled “Signals”When you issue a command like docker stop, Docker communicates with the container using standard Linux signals.
SIGTERM (Graceful Shutdown)
Section titled “SIGTERM (Graceful Shutdown)”When you run docker stop, Docker sends a SIGTERM (Signal Terminate) to PID 1. This tells the application: “Please finish your current web requests, close your database connections, and shut down gracefully.” By default, Docker waits 10 seconds for the application to respect this signal.
SIGKILL (Force Kill)
Section titled “SIGKILL (Force Kill)”If the application does not shut down after the 10-second grace period (or if you run docker kill), Docker sends a SIGKILL. This instantly massacres the process without warning, which can lead to corrupted files or dropped database transactions.
Graceful Shutdown Implementation
Section titled “Graceful Shutdown Implementation”To ensure graceful shutdowns, your application code must be explicitly written to listen for the SIGTERM signal and handle cleanup properly.
6.3 Container Restart Policies
Section titled “6.3 Container Restart Policies”By default, when a container’s main process crashes or exits, the container stops and stays stopped. Restart policies tell the Docker daemon how to handle these failures automatically.
You apply these when creating the container: docker run --restart <policy>
no: The default behavior. Do not automatically restart the container under any circumstances.always: Always restart the container if it stops, regardless of why it stopped. If you manually rundocker stop, Docker will keep it stopped until the Docker daemon itself is restarted, at which point it will boot the container back up.unless-stopped: Similar toalways, but if you manually stop the container, it will not be automatically restarted when the Docker daemon reboots. This is generally the recommended policy for persistent background services.on-failure: Only restart the container if it exits with a non-zero exit code (indicating an error or crash). It will not restart if the process exits cleanly (exit code 0).
6.4 Container Configuration
Section titled “6.4 Container Configuration”Containers are highly customizable at runtime, allowing you to pass specific configurations without rebuilding the underlying image.
Environment Variables
Section titled “Environment Variables”The most common way to pass configuration (like DB endpoints or API keys) into a container.
- Passed via CLI:
docker run -e DB_USER=admin - Passed via File:
docker run --env-file ./config.env
Hostname
Section titled “Hostname”By default, Docker assigns the container’s short ID as its hostname. You can override this for networking or application requirements.
docker run --hostname my-web-server
Containers use the host machine’s DNS settings by default, but you can override them to use specific nameservers (like Google’s or Cloudflare’s) for the container’s outbound traffic.
docker run --dns 8.8.8.8
Working Directory
Section titled “Working Directory”Overrides the WORKDIR defined in the Dockerfile. Defines where commands run inside the container.
docker run -w /tmp
Overrides the USER defined in the Dockerfile. Used to enforce security by ensuring the container does not run as root.
docker run -u 1000:1000
Capabilities
Section titled “Capabilities”By default, Docker drops many dangerous Linux kernel capabilities (like the ability to modify host networks or mount hard drives) to keep the container secure. If your container specifically requires elevated privileges, you can add them (or drop more).
docker run --cap-add NET_ADMINdocker run --privileged(Warning: Gives the container near-root access to the host machine!)