How to use docker container kill command to manage containers

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage Docker containers using the docker container kill command. We will start by creating and running a simple container. Then, you will explore how to stop a running container using the default signal. Finally, you will learn how to kill a container using custom signals, both by name and by number, providing you with essential skills for controlling the lifecycle of your Docker containers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555111{{"How to use docker container kill command to manage containers"}} docker/ps -.-> lab-555111{{"How to use docker container kill command to manage containers"}} docker/stop -.-> lab-555111{{"How to use docker container kill command to manage containers"}} docker/create -.-> lab-555111{{"How to use docker container kill command to manage containers"}} docker/pull -.-> lab-555111{{"How to use docker container kill command to manage containers"}} end

Create and run a simple container

In this step, we will learn how to create and run a simple Docker container. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

First, we need to pull the Docker image we will use. We will use the ubuntu image, which is a minimal Ubuntu operating system.

docker pull ubuntu

This command downloads the ubuntu image from Docker Hub to your local machine. You should see output indicating the download progress.

Now that we have the image, we can run a container based on it. We will run a simple command inside the container.

docker run ubuntu echo "Hello from Docker!"

Let's break down this command:

  • docker run: This is the command to run a new container.
  • ubuntu: This is the name of the image we want to use.
  • echo "Hello from Docker!": This is the command that will be executed inside the container.

When you run this command, Docker will create a new container from the ubuntu image, execute the echo command inside it, and then the container will stop. You should see the output Hello from Docker! in your terminal.

To see the containers that have run, including those that have exited, you can use the docker ps -a command.

docker ps -a

This command lists all containers, both running and stopped. You should see an entry for the container you just ran, with a status of "Exited".

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".

Create and run another container

In the previous step, we killed a running container using the default signal. In this step, we will create and run another container that we will use in the following steps to demonstrate killing a container with custom signals.

We will run another container in detached mode using the ubuntu image and the sleep infinity command, just like before.

docker run -d ubuntu sleep infinity

This command will start a new container in the background that will run indefinitely. Docker will print the new container ID.

To confirm that the container is running, use the docker ps command.

docker ps

You should see the new container listed with a status of "Up". Note down the CONTAINER ID or NAMES of this container. We will use this ID or name in the next steps to kill the container using different signals.

This container is now ready for us to practice sending different signals to it.

Kill the container using a custom signal by name

In the previous step, we started a container that is currently running. In this step, we will learn how to kill this container using a specific signal specified by its name.

The docker kill command allows you to send a specific signal to the main process inside a container. By default, it sends SIGKILL, but you can specify other signals. A common signal used for gracefully shutting down applications is SIGTERM. SIGTERM is a signal that requests a process to terminate. Unlike SIGKILL, processes can catch SIGTERM and perform cleanup operations before exiting.

First, identify the container ID or name of the running container from the previous step using docker ps.

docker ps

Now, use the docker kill command with the -s flag to specify the signal name. We will use SIGTERM. Replace <container_id_or_name> with the actual ID or name of your running container.

docker kill -s SIGTERM <container_id_or_name>

This command sends the SIGTERM signal to the main process of the specified container. If the application inside the container is designed to handle SIGTERM, it will attempt to shut down gracefully. If it doesn't handle SIGTERM or doesn't exit within a certain timeout, Docker will eventually send a SIGKILL to force termination.

To verify that the container has stopped, run docker ps again.

docker ps

The container should no longer be listed as running. You can also use docker ps -a to see its status, which should be "Exited".

Kill the container using a custom signal by number

In the previous step, we killed a container using a signal name (SIGTERM). In this step, we will learn how to kill a container using a signal specified by its number.

Each signal has a corresponding number. For example, SIGKILL is signal 9, and SIGTERM is signal 15. You can find a list of signals and their numbers on a Linux system using the kill -l command.

kill -l

This command will output a list of signal names and their corresponding numbers.

Now, let's run another container that will stay running, similar to the previous steps.

docker run -d ubuntu sleep infinity

Get the container ID or name of this new running container using docker ps.

docker ps

Now, we will use the docker kill command with the -s flag, but this time we will provide the signal number instead of the name. Let's use signal number 9, which corresponds to SIGKILL. Replace <container_id_or_name> with the actual ID or name of your running container.

docker kill -s 9 <container_id_or_name>

This command sends the SIGKILL signal (number 9) to the main process of the specified container. As mentioned before, SIGKILL immediately terminates the process.

To verify that the container has stopped, run docker ps again.

docker ps

The container should no longer be listed as running. You can also use docker ps -a to see its status, which should be "Exited".

Using signal numbers can be useful in scripting or when you need to be precise about the signal being sent.

Summary

In this lab, we learned the fundamental steps of managing Docker containers using the docker container kill command. We began by creating and running a simple container based on the ubuntu image, executing a basic command and observing its exited state using docker ps -a.

Subsequently, we explored how to terminate a running container. We first demonstrated killing a container using the default signal, and then further explored the flexibility of the docker container kill command by terminating containers using both custom signal names and their corresponding numerical values. This hands-on experience provided practical knowledge on controlling the lifecycle of Docker containers.