Docker

Docker Stop Container

Hello. In this tutorial, we will talk about Docker and how “stop” and “kill” container work.

1. What is Docker?

In the present world, Docker is an important term –

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 What is Docker used for?

It is used for –

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.2 Basic Docker terminology?

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates, and runs the Docker containers
  • Hub: A public developer’s registry to distribute their code
  • Repository: A collection of Docker-related images i.e. different versions of the same application

2. How does the docker stop command work?

When you use the docker stop command, Docker sends a SIGTERM signal to the main process inside the container, which is typically the process started by the CMD or ENTRYPOINT directive in the Dockerfile. This signal is a request for the process to gracefully shut down and clean up any resources it is using before exiting. Docker will wait for a default of 10 seconds for the process to exit gracefully. If the process does not exit within this time, Docker will send a SIGKILL signal to forcefully terminate the process and stop the container immediately. You can use the --time or -t option with the docker stop command to specify a different timeout period for waiting for the process to exit gracefully. For example, to wait for 30 seconds, you can run the command docker stop -t 30 your-container-id. It’s important to note that if the process inside the container is not designed to handle the SIGTERM signal and does not exit gracefully, it may leave behind resources such as open files, network connections, or database connections, which can lead to data corruption or other issues. To avoid this, it’s important to ensure that the process inside the container is designed to handle SIGTERM and gracefully shut down when receiving this signal.

2.1 Stop a single container

The command to stop a single container in Docker is:

Command syntax

docker stop your-container-id

Replace your-container-id with the actual ID or name of the container you want to stop. For example, if you want to stop a container with an ID of my-nginx, you can run the following command:

Example

docker stop my-nginx
Fig. 1: Docker stop container command example
Fig. 1: Docker stop container command example

2.2 Stop multiple containers

You can use the docker stop command followed by the names or IDs of the containers you want to stop. Here’s an example:

Command syntax

docker stop container1 container2 container3

In this example, container1, container2, and container3 are the names or IDs of the containers you want to stop. You can specify as many container names or IDs as you want, separated by spaces. Alternatively, you can use a command to stop all running containers at once:

Command syntax #2

docker stop $(docker ps -q)

This command uses the docker ps command with the -q option to get the list of IDs of all running containers, and then passes that list to the docker stop command to stop them all.

3. How does the docker kill command work?

The docker kill command is used to forcibly stop a running container by sending a SIGKILL signal to the main process running inside the container. This command is different from the docker stop command which sends a SIGTERM signal to the main process to allow it to perform a graceful shutdown before stopping the container. When you run docker kill your-container_name_or_id, Docker immediately sends a SIGKILL signal to the main process running inside the container. This signal immediately terminates the process without allowing it to perform any cleanup or save any data. If the process running inside the container has set up signal handlers to handle the SIGKILL signal or has ignored the signal, the docker kill command may not be able to stop the container immediately. In such cases, you may need to use the --force or -f option to forcibly kill the container. It’s important to note that using docker kill is an extreme measure and should be used only when you need to forcibly stop a container that is not responding to docker stop command. In general, you should always try to use the docker stop command first to allow the container to perform a graceful shutdown before resorting to docker kill.

3.1 Kill a single container

You can use the docker kill command followed by the name or ID of the container you want to kill. Here’s an example:

Command syntax

docker kill container_name_or_id

In this example, container_name_or_id is the name or ID of the container you want to kill. If the container is running a long-running process, you may need to wait for a few seconds after running the docker kill command to ensure that the container has stopped completely. It’s important to note that using docker kill is an extreme measure and should be used only when you need to forcibly stop a container that is not responding to docker stop command. In general, you should always try to use the docker stop command first to allow the container to perform a graceful shutdown before resorting to docker kill.

Example

docker kill my-nginx
Fig. 2: Docker kill container command example
Fig. 2: Docker kill container command example

3.2 Killing multiple containers

To kill multiple containers at once, you can use the docker kill command followed by the names or IDs of the containers you want to kill. Here’s an example:

Command syntax

docker kill container1 container2 container3

In this example, container1, container2, and container3 are the names or IDs of the containers you want to kill. You can specify as many container names or IDs as you want, separated by spaces. If the containers are running long-running processes, you may need to wait for a few seconds after running the docker kill command to ensure that the containers have stopped completely. Again, it’s important to note that using docker kill is an extreme measure and should be used only when you need to forcibly stop containers that are not responding to docker stop command. In general, you should always try to use the docker stop command first to allow the containers to perform a graceful shutdown before resorting to docker kill.

That concludes this tutorial, and I hope that it provided you with the information you were seeking. Enjoy your learning journey, and don’t forget to share!

4. Summary

In conclusion, Docker is a powerful platform that simplifies the process of deploying and managing applications in a containerized environment. It provides a way to package applications and their dependencies into a single image that can be easily shared across different environments.

The docker stop command is used to stop a running container gracefully by sending a SIGTERM signal to the process running inside the container. This allows the process to clean up resources and gracefully shut down before the container is stopped.

On the other hand, the docker kill command is used to forcefully stop a running container by sending a SIGKILL signal to the process running inside the container. This immediately terminates the process and stops the container without giving it a chance to clean up resources or gracefully shut down. It is important to use these commands appropriately based on your use case. If you need to gracefully stop a container and allow it to clean up resources before stopping, then use docker stop. However, if you need to immediately stop a container and do not care about the cleanup process, then use docker kill.

Overall, understanding how to use these Docker commands effectively is crucial for managing containers and ensuring the smooth operation of your applications. You can also download the commands used in this tutorial from the Downloads section.

5. Download the Project

This was a tutorial on how “stop” and “kill” work in Docker.

Download
You can download the full source code of this example here: Docker Stop Container

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button