Difference between docker stop and docker kill commands
I. Overview
Docker is an operating system-level software framework for creating, managing, and running containers on servers and the cloud. Docker supports stopping containers in two different ways.
In this tutorial, we will learn to stop and terminate containers using docker stop
and commands.docker kill
We will use different docker commands to stop and delete containers.
2. Comprehension docker stop
and docker kill
Command
Starting and stopping a container is not the same as starting and stopping a normal process. To terminate a container, Docker provides docker stop
and docker kill
commands. docker kill
and docker stop
commands look similar, but their internal execution is different.
docker stop
Commands send the SIGTERM signal, and docker kill
commands send the SIGKILL signal. Execution of SIGTERM and SIGKILL. is different. Unlike SIGKILL, SIGTERM terminates the process gracefully rather than immediately. The SIGTERM signal can be handled, ignored, or blocked, but the SIGKILL signal cannot be blocked or handled. SIGTERM allows the child or parent process the opportunity to send information to other processes.
With SIGKILL, we may create zombie processes because the killed child process cannot notify its parent that it received the kill signal. It will take some time for the container to shut down completely.
3. Execution docker stop
and docker kill
commands
Before we go any further docker stop
, docker kill
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 Digest: sha256:2c954f8c5d03da58f8b82645b783b56c1135df17e650b186b296fa1bb71f9cfd Status: Downloaded newer image for postgres:latest 0aece936b317984b5c741128ac88a891ffc298d48603cf23514b7baf9eeb981a
Let's see the details 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
docker ps
command to list all running processes on the host.
3.1. docker stop
Order
docker stop
The command gracefully stops the container and provides a safe way out. If docker stop
the command fails to kill the process within the specified timeout, Docker implicitly issues the kill command immediately. In Docker, we can use docker stop
commands in two ways to stop a process. We can use containerId
or container name to stop the container.
Let's demonstrate stopping a container using the container name:
$ docker stop postgresql-baeldung
Let's use an example to containerId
stop the container:
$ docker stop be2848539d76
Interestingly, we can also use the containerId prefix to start or stop a container. Here, we just need to make sure that no other containers are containerId
running with "be" as startup:
$ docker stop be
By default, the docker stop
command waits 10 seconds to kill the process. But we can -t
configure the wait time with options:
$ docker stop -t 60 be2848539d76
Here, the container will wait 60 seconds before force removing the container. We can also docker container stop
stop the container with the command:
$ docker container stop -t 60 be2848539d76
Both commands work exactly the same way. Docker docker container stop
commands are deprecated in newer versions of Docker.
3.2. docker kill
Commands
docker kill
The command abruptly terminated the entrypoint process. docker kill
command will cause an unsafe exit. In some cases, docker containers run with volumes mounted with the host. This can lead to filesystem corruption if there are still pending changes in memory when the main process is stopped.
Let's see 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
docker container kill
Commands work docker kill
similarly to commands.
4. Additional commands to stop the container
docker kill
and docker stop
command will stop the container. Another way to stop a container is to delete it. We can use the docker rm
command to remove a container. This will immediately remove the container from local storage:
$ docker rm be2848539d76
When we run docker rm
the command, the container is removed from the docker ps -a
list. While using docker stop
commands, we can reserve containers for reuse. Ideally, we can put the container in two states. A container can enter a stopped or paused state. If a container is stopped, all resources it allocated are freed, while a suspended container does not free memory, but CPU. In this case, the process is suspended.
Let's see the command to pause the container:
$ docker pause be2848539d76
It's worth noting that we can inspect the container details even after the container is stopped. To learn more about containers, we can use the docker inspect
command. docker inspect
The command displays the container's exit code in the container state. When using the docker stop
command to stop the container, the exit code is 0. Likewise, the docker kill
command displays the container status as a non-zero exit code.
5 Conclusion
In this tutorial, we discussed the difference between execute docker stop
and docker kill
command. First, we discussed using different commands to stop containers. Next, we discussed the implementation of SIGTERM and SIGKILL in the docker stop and docker kill commands.
We also explored the different options for these two commands. Later, we explored docker container stop,
and docker container kill
ordered.
In short, we learned various ways to stop and kill Docker containers.
0 Comments