Stop the container using the default signal and timeout
In this step, you will learn how to stop a running Docker container using the default signal and timeout. When you stop a container, Docker sends a signal to the main process running inside the container. By default, Docker sends the SIGTERM
signal, which tells the process to shut down gracefully. If the process does not exit within a default timeout period (usually 10 seconds), Docker sends a SIGKILL
signal to force the process to terminate.
First, let's run a container that will stay running. We will use the ubuntu
image and run a simple command that keeps the container alive.
docker pull ubuntu
You should see output indicating that the ubuntu
image is being pulled.
Using default tag: latest
latest: Pulling from library/ubuntu
...
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
Now, run the ubuntu
container in detached mode (-d
) so it runs in the background, and execute a command that will keep it running indefinitely (e.g., tail -f /dev/null
).
docker run -d ubuntu tail -f /dev/null
This command will output the container ID.
<container_id>
You can verify that the container is running using the docker ps
command:
docker ps
You should see the ubuntu
container listed with a status of Up
.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<container_id> ubuntu "tail -f /dev/null" About a minute ago Up About a minute ago <container_name>
Now, let's stop this running container using the docker stop
command. You can use either the container ID or the container name. Replace <container_id>
with the actual ID of your running container.
docker stop <container_id>
The command will output the container ID that was stopped.
<container_id>
After running the docker stop
command, the container will receive the SIGTERM
signal. Docker will wait for the default timeout (10 seconds) for the container to stop gracefully. If it doesn't stop within that time, it will send SIGKILL
.
You can verify that the container has stopped by running docker ps
again.
docker ps
The ubuntu
container should no longer be listed in the output of docker ps
(which only shows running containers). To see all containers, including stopped ones, use docker ps -a
.
docker ps -a
You should see the ubuntu
container listed with a status of Exited
.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<container_id> ubuntu "tail -f /dev/null" About a minute ago Exited (0) About a minute ago <container_name>
This confirms that the container was successfully stopped using the default signal and timeout.