Stop a container in Docker
Learn how to safely stop a running Docker container using the docker stop command, timeout options, and status checks.
Lesson Content
When you need to stop a running Docker container, you can use the docker container stop
command. Docker aliases this as docker stop
, so these commands do exactly the same thing:
docker container stop myapp
docker stop myapp
The docker stop
version is much more common, because it’s simpler and easier to type.
What happens when you stop a container
We just stopped a container so… what just happened?
Well, when we executed docker stop
, Docker sent a SIGTERM
signal to the main process in your container. This politely asked the app to shut down, gracefully, which saves its state, closes any open connections, and finishes writing to any files.
The container doesn't disappear though — it just moves to a stopped state.
You can verify this by running:
# Get all running and non-running containers
docker ps -a
# Get just a specific container by name with a filter
docker ps -af "name=myapp"
The -a
flag shows all containers, including stopped ones. Your container is still there, ready to be started again with all its configuration intact.
Control the shutdown timeout
By default, Docker gives the container 10 seconds to shut down gracefully. If it doesn't stop in time, Docker then sends a SIGKILL
command to the container to force it to shut down.
You can adjust this timeout with the -t
(or --timeout
) flag:
docker stop -t 30 myapp # wait 30 seconds
docker stop -t 5 myapp # wait 5 seconds
Databases and stateful apps typically need more time to shut down cleanly, but services like web servers can usually stop pretty quickly.
If you need to force an immediate stop (note: not recommended for production), an easy way to do this is to just set the timeout to zero:
docker stop -t 0 myapp # immediate SIGKILL
For more advanced stop methods, look into stopping Docker container by using signals.