How to Stop a Container in Docker: Simple Commands
To stop a running Docker container, use the
docker stop <container_id_or_name> command which gracefully stops the container. If the container does not stop, use docker kill <container_id_or_name> to forcefully terminate it.Syntax
The basic command to stop a Docker container is docker stop <container_id_or_name>. Here, container_id_or_name is the unique ID or the name of the container you want to stop.
If the container does not stop within a timeout period, you can use docker kill <container_id_or_name> to immediately stop it.
bash
docker stop <container_id_or_name> docker kill <container_id_or_name>
Example
This example shows how to stop a running container named my_app. First, list running containers to find the container name or ID, then stop it.
bash
docker ps # Output shows running containers # CONTAINER ID IMAGE COMMAND NAMES # a1b2c3d4e5f6 nginx "nginx -g 'daemon off;'" my_app docker stop my_app # Stopping container 'my_app' gracefully
Output
CONTAINER ID IMAGE COMMAND NAMES
a1b2c3d4e5f6 nginx "nginx -g 'daemon off;'" my_app
my_app
Common Pitfalls
One common mistake is trying to stop a container that is not running, which results in an error. Another is using docker kill without understanding it forcefully stops the container without cleanup.
Always try docker stop first to allow the container to exit cleanly.
bash
docker stop non_running_container # Error: No such container: non_running_container docker kill my_app # Immediately stops the container without graceful shutdown
Output
Error: No such container: non_running_container
my_app
Quick Reference
| Command | Description |
|---|---|
| docker stop | Gracefully stops a running container |
| docker kill | Forcefully stops a container immediately |
| docker ps | Lists running containers to find container ID or name |
Key Takeaways
Use
docker stop to gracefully stop a running container by name or ID.If
docker stop fails, use docker kill to force stop the container.Always check running containers with
docker ps before stopping.Stopping a non-running container causes an error; verify container status first.
Force stopping skips cleanup, so prefer graceful stop when possible.