Docker Curriculum
On this page
- Starting Stopping Containers
- Starting Containers
- docker run — Create and Start
- docker start — Start an Existing Stopped Container
- docker create — Create Without Starting
- Stopping Containers
- docker stop — Graceful Shutdown
- When to Increase the Grace Period
- Your Application Must Handle SIGTERM
- docker kill — Immediate Termination
- Restarting Containers
- Pausing and Unpausing
- Monitoring Start and Stop Events
- Production Patterns
- Pattern 1: Systemd-Managed Docker Container
- Pattern 2: AWS ECS (Production Standard)
- Summary
Starting Stopping Containers
Starting and stopping containers is something you do dozens of times per day. But there is much more to it than just docker start and docker stop. How you start a container determines its behavior, resource usage, and how it integrates with the rest of your system. How you stop it determines whether your application shuts down cleanly or loses in-flight work.
This file covers the complete picture — every option for starting containers, the correct way to stop them, and the patterns used in real production environments on AWS.
Starting Containers
docker run — Create and Start
docker run creates a new container from an image and starts it immediately. This is the most common way to start a container.
docker start — Start an Existing Stopped Container
docker start starts a container that already exists but is stopped. The container was previously created with docker run or docker create.
Unlike docker run, docker start does not let you change container configuration. The ports, environment variables, and volumes are fixed from when the container was created. If you need to change configuration, you must remove and recreate the container.
docker create — Create Without Starting
docker create creates a container but does not start it. Useful when you need to copy files into the container before it starts.
Stopping Containers
docker stop — Graceful Shutdown
docker stop sends SIGTERM to the container's main process (PID 1). This gives the process a chance to shut down cleanly — finish in-flight requests, flush buffers, close database connections, write pending data.
After a configurable grace period (default 10 seconds), if the process has not exited, Docker sends SIGKILL to force termination.
When to Increase the Grace Period
The default 10-second grace period is often too short for:
- Web servers finishing long-running requests
- Workers processing a job that should not be interrupted
- Applications flushing large write buffers
Your Application Must Handle SIGTERM
For graceful shutdown to work, your application must listen for SIGTERM and shut down cleanly when it receives it.
Python Flask with Gunicorn:
Gunicorn handles SIGTERM automatically. It finishes in-flight requests and exits.
Node.js:
Python (general):
docker kill — Immediate Termination
docker kill sends SIGKILL directly — the process is terminated immediately with no opportunity to clean up.
Only use docker kill when docker stop is not working — the container is frozen or not responding. Killing without warning can cause data loss, incomplete transactions, and corrupt state.
Restarting Containers
Restarting is equivalent to docker stop followed by docker start. The container configuration is preserved — same ports, volumes, environment variables.
Use cases:
- Apply a new configuration file that was mounted via volume (app reads config on startup)
- Recover a container that is running but in a degraded state
- Force a connection pool to re-establish database connections after a database failover
Pausing and Unpausing
Pausing a container freezes all its processes while keeping the container's state in memory.
Use cases:
- Temporarily free CPU on an overloaded host
- Take a consistent snapshot of a container's in-memory state
- Pause a consumer while performing maintenance on the queue it reads from
Paused containers still consume memory — they just use no CPU.
Monitoring Start and Stop Events
Docker emits events for container state changes. You can monitor them in real time:
This is useful for debugging unexpected restarts and understanding your container's history.
Production Patterns
Pattern 1: Systemd-Managed Docker Container
On a standalone EC2 instance, you can manage a Docker container with systemd so it starts on boot and restarts on failure:
Pattern 2: AWS ECS (Production Standard)
On AWS ECS, you define how containers start, stop, and recover in a task definition:
ECS handles starting, stopping, health checking, and restarting containers automatically based on this definition.
Summary
docker run -dcreates and starts a container in the background — use this for servicesdocker startstarts an existing stopped container — configuration cannot be changeddocker stopsendsSIGTERMfor graceful shutdown — your application must handle this signal- Increase the stop timeout (
--time 30) for applications that need more time to finish in-flight work docker killsendsSIGKILLfor immediate termination — only use whendocker stopdoes not workdocker restartcombines stop and start — useful for applying new configurationdocker pauseanddocker unpausefreeze and resume processes without stopping the container- In production, ECS manages container start/stop with defined health checks and stop timeouts — prefer ECS over manually running containers on EC2
In the next file, we cover container logs in depth — reading, following, filtering, and routing logs to AWS CloudWatch.