Difference between docker stop and docker kill commands
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 stopand commands.
We will use different docker commands to stop and delete containers.
Starting and stopping a container is not the same as starting and stopping a normal process. To terminate a container, Docker provides
docker stopcommands look similar, but their internal execution is different.
docker stopCommands send the SIGTERM signal, and
docker killcommands 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.
Before we go any further
docker killlet'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 pscommand to list all running processes on the host.
docker stopThe command gracefully stops the container and provides a safe way out. If
docker stopthe command fails to kill the process within the specified timeout, Docker implicitly issues the kill command immediately. In Docker, we can use
docker stopcommands in two ways to stop a process. We can use
containerIdor 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
containerIdstop 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
containerIdrunning with "be" as startup:
$ docker stop be
By default, the
docker stopcommand waits 10 seconds to kill the process. But we can
-tconfigure 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 stopstop the container with the command:
$ docker container stop -t 60 be2848539d76
Both commands work exactly the same way. Docker
docker container stopcommands are deprecated in newer versions of Docker.
docker killThe command abruptly terminated the entrypoint process.
docker killcommand 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 killcommand:
$ docker container kill be2848539d76
docker container killCommands work
docker killsimilarly to commands.
4. Additional commands to stop the container
docker stopcommand will stop the container. Another way to stop a container is to delete it. We can use the
docker rmcommand to remove a container. This will immediately remove the container from local storage:
$ docker rm be2848539d76
When we run
docker rmthe command, the container is removed from the
docker ps -alist. While using
docker stopcommands, 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 inspectThe command displays the container's exit code in the container state. When using the
docker stopcommand to stop the container, the exit code is 0. Likewise, the
docker killcommand displays the container status as a non-zero exit code.
In this tutorial, we discussed the difference between execute
docker killcommand. 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 killordered.
In short, we learned various ways to stop and kill Docker containers.