Kill the running container using default signal
In the previous step, we ran a container that executed a command and then exited. In this step, we will run a container that stays running and then learn how to stop it using the default signal.
First, let's run a container that will keep running. We will use the ubuntu
image again, but this time we will run a command that waits indefinitely.
docker run -d ubuntu sleep infinity
Let's look at the command:
docker run
: Command to run a new container.
-d
: This flag runs the container in detached mode, meaning it runs in the background and doesn't block your terminal.
ubuntu
: The image to use.
sleep infinity
: The command to run inside the container. sleep infinity
is a command that will cause the container to run indefinitely.
After running this command, Docker will print the container ID. This container is now running in the background.
To see the running containers, use the docker ps
command.
docker ps
You should see an entry for the container you just started, with a status of "Up" followed by the duration it has been running. Note the CONTAINER ID
or NAMES
of this container, as we will need it to stop the container.
Now, let's stop the running container using the docker kill
command. By default, docker kill
sends the SIGKILL
signal to the container's main process. SIGKILL
is a signal that immediately terminates a process and cannot be caught or ignored.
Replace <container_id_or_name>
with the actual ID or name of your running container from the docker ps
output.
docker kill <container_id_or_name>
After running this command, the container should stop. You can verify this by running docker ps
again. The container should no longer appear in the list of running containers.
To see the stopped container, you can use docker ps -a
. Its status should now be "Exited".