How to use docker compose kill command to force stop services

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker compose kill command to forcefully stop services defined in a Docker Compose file. We will begin by preparing a simple Compose file with multiple services and starting them using docker compose up.

Subsequently, you will explore different ways to use the docker compose kill command: to force stop all services, to force stop a specific service, and to force stop services using a different signal. This hands-on experience will provide you with practical knowledge of managing Docker Compose services using the kill command.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/logs("View Container Logs") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/ps -.-> lab-555081{{"How to use docker compose kill command to force stop services"}} docker/logs -.-> lab-555081{{"How to use docker compose kill command to force stop services"}} docker/pull -.-> lab-555081{{"How to use docker compose kill command to force stop services"}} end

Prepare a simple Compose file with multiple services

In this step, we will prepare a simple Docker Compose file that defines multiple services. Docker Compose is a tool that allows you to define and run multi-container Docker applications. While Docker is pre-installed on this VM, Docker Compose is not. We will install it first.

First, let's install Docker Compose. We will download the latest stable release and make it executable.

sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Now, let's verify the installation by checking the version.

docker-compose --version

You should see the version information printed to the console, confirming that Docker Compose is installed correctly.

Next, we will create a directory for our project and navigate into it.

mkdir ~/project/my-compose-app
cd ~/project/my-compose-app

Now, we will create a docker-compose.yml file in this directory. This file will define our services. We will use the nano editor to create and edit the file.

nano docker-compose.yml

Inside the nano editor, paste the following content. This file defines two services: web and redis. The web service uses the nginx image and maps port 80 of the container to port 8080 on the host. The redis service uses the redis image.

version: "3.8"
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  redis:
    image: redis

After pasting the content, save the file by pressing Ctrl + X, then Y, and finally Enter.

Before we can start the services, we need to pull the images defined in the docker-compose.yml file. We can do this using the docker pull command.

docker pull nginx
docker pull redis

These commands will download the nginx and redis images from Docker Hub.

Start the services using docker compose up

In this step, we will start the services defined in our docker-compose.yml file using the docker compose up command. This command builds, (re)creates, starts, and attaches to containers for a service.

Make sure you are in the ~/project/my-compose-app directory where you created the docker-compose.yml file.

cd ~/project/my-compose-app

Now, run the following command to start the services. The -d flag runs the containers in detached mode, meaning they will run in the background and not tie up your terminal.

docker-compose up -d

You should see output indicating that the networks, volumes (if any), and containers are being created and started.

To verify that the containers are running, you can use the docker ps command.

docker ps

This command lists all running containers. You should see entries for both the web (nginx) and redis containers.

You can also check the logs of the services using the docker-compose logs command.

docker-compose logs

This will display the logs for all services. You can also view logs for a specific service by adding the service name, for example, docker-compose logs web.

Finally, since we mapped port 80 of the nginx container to port 8080 on the host, you can access the default Nginx welcome page by using curl to access localhost on port 8080.

curl localhost:8080

You should see the HTML content of the Nginx welcome page in the output.

Force stop all services using docker compose kill

In this step, we will learn how to force stop all running services defined in our docker-compose.yml file using the docker compose kill command. Unlike docker compose down which gracefully stops containers and removes resources, docker compose kill sends a specified signal to the containers to force them to stop immediately.

Make sure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app

To force stop all services, simply run the docker compose kill command without specifying any service names. By default, this command sends a SIGKILL signal to the containers.

docker-compose kill

You should see output indicating that the containers are being killed.

To verify that the containers have been stopped, you can use the docker ps command again.

docker ps

This time, you should not see the web (nginx) and redis containers listed, as they have been stopped.

You can also use docker ps -a to see all containers, including those that are stopped. You should see the my-compose-app-web-1 and my-compose-app-redis-1 containers with a status of "Exited".

docker ps -a

Force stop a specific service using docker compose kill

In this step, we will learn how to force stop a specific service defined in our docker-compose.yml file using the docker compose kill command followed by the service name.

First, let's start our services again so we have containers to stop. Make sure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app
docker-compose up -d

Verify that both containers are running using docker ps.

docker ps

Now, let's force stop only the web service. We do this by adding the service name web after the docker compose kill command.

docker-compose kill web

You should see output indicating that the web container is being killed.

To verify that only the web container has been stopped, use the docker ps command again.

docker ps

You should now only see the redis container listed as running. The web container should not be in the list of running containers.

You can also use docker ps -a to see all containers, including stopped ones. The my-compose-app-web-1 container should have a status of "Exited", while the my-compose-app-redis-1 container should still be "Up".

docker ps -a

Force stop services with a different signal using docker compose kill -s

In this step, we will explore how to use the docker compose kill command with the -s flag to send a specific signal to the containers. By default, docker compose kill sends a SIGKILL signal, which immediately terminates the process. However, you might want to send a different signal, such as SIGTERM, which is a request to terminate the process gracefully.

First, let's ensure our services are running. Navigate to the project directory and start the services in detached mode.

cd ~/project/my-compose-app
docker-compose up -d

Verify that both containers are running using docker ps.

docker ps

Now, let's force stop all services using the SIGTERM signal. We use the -s flag followed by the signal name.

docker-compose kill -s SIGTERM

You should see output indicating that the containers are being killed with the specified signal.

To confirm that the containers have stopped, use the docker ps command.

docker ps

Neither the web nor the redis container should be listed as running.

Using docker ps -a will show that the containers have exited.

docker ps -a

The ability to send different signals is useful for more controlled shutdowns, allowing applications within the containers to perform cleanup tasks before exiting, if they are configured to handle those signals.

Finally, to clean up the stopped containers and the network created by Docker Compose, you can use the docker compose down command.

docker-compose down

This command will stop and remove the containers, networks, and volumes defined in the docker-compose.yml file.

Summary

In this lab, we learned how to prepare a simple Docker Compose file defining multiple services (web and redis) and installed Docker Compose. We then started these services using the docker compose up command. Subsequently, we explored how to forcefully stop all running services defined in the Compose file using docker compose kill, and how to target a specific service for forceful termination with docker compose kill <service_name>. Finally, we learned how to send a different signal to services during the kill process using the -s flag, such as docker compose kill -s SIGKILL.