Docker stop or kill command

In this tutorial we will learn about how we can stop or kill the running docker containers. We will be using docker run command to create and start a container if you do not know about anything regarding this command please read below article first:

Docker run command

How to create and run a container from docker image?

First of all to learn about stopping or killing a running container we first need to create and then start a container. Let's create a new container from docker image called busybox.

This image allow us to run some linux command inside the container. Open your terminal window and run following command:

# check to see if we have local docker image
# if we do not have one download from the docker hub
# create a container out of this docker image and start
# the container by showing process logs on our terminal
docker run busybox

# output of above command
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
322973677ef5: Pull complete 
Digest: sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084
Status: Downloaded newer image for busybox:latest

If you do not want to use above command you can do the same thing by running following sets of command:

# get busybox image from dockerhub
docker pull busybox

# create a new container from the this image
docker create busybox

# get the container id from output of the above command
docker start d95970936b265fa31b4f53025bf8fc762fcc23edb85ef23d32ab985c83617cb9

Alright now we have busy box image on our local machine. This image does nothing however we can run some linux command by adding an argument to docker command.

Let's create a new container out of busybox docker image and add a ping command so that our container is constantly running.

# create a new container from busybox image
# allow container to ping on google.com
docker create busybox ping google.com

# above command will create a new container and
# output the newly created container ID
# let's start this container so that it is keep running
docker start 8ccb2a811121333a8be38ed25195000522c76e596a10f21c6991c819076afc8f

# above command will output the same container id 
# however you will see no other output regarding ping command
# this is because it is running a container in the background
# and we are not attaching a container process output on our terminal
# to see if our container is running try following command
docker ps

# above command will list all the running containers
# following is the output of the above command
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8ccb2a811121        busybox             "ping google.com"   2 minutes ago       Up About a minute                       unruffled_almeida

So far we have a container that is running. Now, we want to stop or kill this container.

How to stop the running container?

Docker stop command sends SIGTERM signal to running container process. It will stop the process however it takes a while to shutdown the container completely.

Let's look at the syntax of this command:

Let's now stop our running container and make sure it is stopped. Open your terminal window and type following commands:

# let's find our running container
docker ps

# grab the CONTAINER ID of our running container
# and stop our container
docker stop 8ccb2a811121

# you will notice it takes few seconds to stop
# the running container let's check if container is stopped
docker ps

After running docker ps command you will see no container is running because we stopped the container using docker stop command. One thing to notice here that it takes some pause before it actually stops the container.

How to kill the running container?

Docker kill command is kind of similar to docker stop command however it sends SIGKILL  signal to our running container process. SIGKILL signal immediately shuts the container down without taking any pause.

Let's look at the syntax of this command:

Let's now use docker kill command to shutdown our running container. Run following commands to start the container and then kill the running container and notice time it takes to shutdown the container compare to docker stop command:

# create a new container from busybox image
# allow container to ping on google.com
docker create busybox ping google.com

# get the container id from above command output
# start the container with container id
docker start 52fc0df3e4a192c5adb48efa5d1f5d8d6c3a8833aa8ee622a817915060989846

# check the running container
docker ps

# you will see following output
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
52fc0df3e4a1        busybox             "ping google.com"   55 seconds ago      Up 15 seconds                           fervent_ritchie

# grab the container id and now
# kill the running container and notice time it takes
docker kill 52fc0df3e4a1

What is the difference between docker stop and kill command?

What’s the difference between docker stop and docker kill ? They will both stop a running container right? Here is the difference?

  • docker stop: Container process receives SIGTERM signal first, after sometime it receives s SIGKILL signal
  • docker kill: Container process receives SIGKILL signal and stops immediately