You stop a running container using the docker stop command followed by the container ID or name, which sends a SIGTERM signal to allow graceful shutdown before forcibly stopping with SIGKILL after a timeout.
The docker stop command is the primary way to gracefully stop a running container. It sends a SIGTERM signal to the container's main process, allowing it time to clean up resources, save state, and shut down properly. If the container hasn't stopped after the configured grace period (default 10 seconds), Docker sends a SIGKILL signal to force termination. This approach balances clean shutdown with eventual termination, making it safer than abruptly killing containers.
After issuing docker stop, the container transitions from a running state to exited. You can verify this with docker ps -a. The container's filesystem and logs remain available unless you used --rm when starting it. The default 10-second timeout is configurable per container at runtime using the --stop-timeout flag, or globally by adjusting the Docker daemon's configuration.
For cases where a container is unresponsive to SIGTERM or you need immediate termination, docker kill sends a SIGKILL signal directly, bypassing the grace period. This should be a last resort, as it doesn't allow the application to perform cleanup tasks. You can also use docker kill --signal=SIGHUP to send custom signals for specific application behavior like log rotation or configuration reload.
docker stop: Sends SIGTERM, waits 10 seconds (by default), then SIGKILL if needed. Preferred for normal shutdown.
docker kill: Sends SIGKILL immediately. Use for unresponsive containers or when immediate termination is required.
docker kill --signal=SIGTERM: Same behavior as docker stop but without the automatic SIGKILL fallback.
Ctrl+C in foreground terminal: Equivalent to docker stop for the container running in the foreground.
When stopping containers in production environments, ensure your application handles SIGTERM gracefully. Well-behaved containers should catch this signal, close database connections, flush logs, and complete any in-progress operations before exiting. Docker provides the --stop-signal flag to specify which signal to use, allowing you to customize shutdown behavior for applications that don't respond to SIGTERM.