Docker is an os-level software framework used to create, manage and run containers on servers and in the cloud. Docker provides support to stop the container using two different ways.
In this tutorial, we'll learn to stop and kill the container using the docker stop and docker kill commands.
We'll use different docker commands to stop and remove a container.
2. Understanding the docker stop and docker kill Commands
Starting and stopping a container is not the same as starting and stopping a normal process. To terminate a container, Docker provides the docker stop and docker kill commands. Both the docker kill and docker stop commands look similar, but their internal execution is different.
The docker stop commands issue the SIGTERM signal, whereas the docker kill commands sends the SIGKILL signal. The execution of the SIGTERM and SIGKILL. is different. Unlike SIGKILL, the SIGTERM gracefully terminates a process rather than killing it immediately. It is possible to handle, ignore or block a SIGTERM signal, but a SIGKILL signal cannot be blocked or handled. SIGTERM allows a child process or parent process the opportunity to send information to other processes.
With SIGKILL, we may create zombie processes as the killed child process doesn't get to notify its parent that it received the kill signal. The container needs some time to shut down completely.
3. Executing the docker stop and docker kill Command
Before we move forward to understand the docker stop and docker kill commands, let's first run a sample Postgres Docker container:
$ docker run -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung
-p 5432:5432 -v /data:/var/lib/postgresql/data --name postgresql-baedlung postgres
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
214ca5fb9032: Pull complete
95df4ec75c64: Pull complete
Status: Downloaded newer image for postgres:latest
Let's check out the detail of the container:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be2848539d76 postgres "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 0.0.0.0:5432->5432/tcp postgresql-baedlung
The docker ps command lists all the running processes on the host machine.
3.1. Docker Stop COMMAND
The docker stop command stops the container gracefully and provides a safe way out. If a docker stop command fails to terminate a process within the specified timeout, the Docker issues a kill command implicitly and immediately. In Docker, there are two ways we can use the docker stop command to stop a process. We can use containerId or container name to stop a container.
Let's demonstrate to stop a container using the container name:
$ docker stop postgresql-baeldung
Let's illustrate to stop a container using the containerId:
$ docker stop be2848539d76
Interestingly, we can also use the prefix of containerId to start or stop a container. Here, we just need to make sure that no other container is running with “be” as starting containerId:
$ docker stop be
By default, the docker stop command waits 10 seconds to kill the process. But we can configure the wait time using the -t option:
$ docker stop -t 60 be2848539d76
Here, the container will wait for 60 seconds before forcefully removing the container. We can also stop a container using the docker container stop command:
$ docker container stop -t 60 be2848539d76
Both the commands work exactly in the same way. The docker container stop command is deprecated in the newer version of the Docker.
3.2. Docker Kill COMMAND
The docker kill command terminates the entry point process abruptly. The docker kill command causes an unsafe exit. In some cases, the docker container runs with the volume mounted with the host. This might result in damage to the file system if there are still pending changes in memory when the main process stops.
Let's look into the command to kill a container:
$ docker kill be2848539d76
Similarly, to kill a container, we can also use the docker container kill command:
$ docker container kill be2848539d76
The docker container kill command works similarly to the docker kill command.
4. Additional Commands to Stop a Container
Both the docker kill and docker stop commands stop a container. One more way to stop a container is just to remove it. We can use the docker rm command to remove a container. This will remove the container from local storage immediately:
$ docker rm be2848539d76
When we run the docker rm command, the container is removed from the docker ps -a list. While using the docker stop command, we can keep the container to reuse. Ideally, we can put the container into two states. The container can be out into a stopped or paused state. If a container is stopped, all its allocated resources are released, while a container that is paused does not release memory, but the CPU is released. In that case, the process is suspended.
Let's check out the command to pause a container:
$ docker pause be2848539d76
Notably, we can check the details of the container even after it is stopped. To find out more about a container, we can use the docker inspect command. The docker inspect command shows the exit code of the container under container status. The exit code is 0 when the container is stopped using the docker stop command. Similarly, the docker kill command shows the container status as a non-zero exit code.
In this tutorial, we discussed the difference between the execution of the docker stop and the docker kill command. First, we discussed stopping a container using a different command. Next, we discussed the implementation of SIGTERM and SIGKILL in the docker stop and docker kill commands.
We also explored the different options of both commands. Later, we explored the docker container stop, and docker container kill command.
In short, we learned various ways to stop and kill Docker containers.
res – REST with Spring (eBook) (everywhere)