How to Stop Docker Containers

July 23, 2024

Introduction

The lifetime of a Docker container depends on how long its default command runs. Once the command stops running, Docker automatically stops the container.

Some containers, such as those running web servers, are designed to run indefinitely. When users decide they no longer need such containers, they must stop them manually. The same applies to containers in an error state.

This tutorial will show you how to stop Docker containers.

How to stop Docker containers.

Prerequisites

  • Command-line access.
  • Administrative privileges on the system.
  • Docker installed.

Stop Docker Container Gracefully (docker stop)

The recommended way to stop Docker containers is to use the docker stop command, which provides time (ten seconds by default) for the container to gracefully terminate its processes before quitting. The grace period avoids potential problems related to the abrupt termination of containers.

The syntax for the docker stop command is:

docker stop [option] [container-name-or-id]

For example, to stop a container named test-stop, enter the following:

docker stop test-stop

If successful, the command echoes back the name of the stopped container.

A successful docker stop command prints the name of the container in its output.

To stop a container with the ID 7a4cfbea094b, type the command below:

docker stop 7a4cfbea094b
A successful docker stop command prints the ID of the container in its output.

Tip: Docker container names and IDs can be found by listing Docker containers.

docker stop Options

docker stop features two options that can be appended as arguments to the main command:

  • The -s (--signal) option allows sending a custom system call signal to the container. The flag accepts the signal's name (e.g., SIGINT) or a number corresponding to its place in the syscall table.
  • The -t (--time) enables the user to customize the duration of the grace period before Docker kills the container. The option accepts values in seconds.

Force Stop Docker Container (docker kill)

When the container is in an error state, it may become unresponsive and refuse to stop. The docker kill command provides a way to force quit a misbehaving container.

Below is the syntax of the docker kill command:

docker kill [option] [container-name-or-id]

For example, type the following command to force quit the container named test-kill:

docker kill test-kill

The output echoes back the name of the container.

A successful docker kill command prints the name of the container in its output.

docker kill Options

The docker kill command accepts only the -s (--signal) option as a command argument. As in the case of docker stop, this option allows the user to pass a system call signal to the container.

docker stop vs. docker kill

Both docker stop and docker kill can terminate a running container. However, they differ in how they handle the exiting process:

  • The docker stop command sends the SIGTERM signal (or another stop signal, such as SIGQUIT, defined in the image metadata) and waits for the container to stop gracefully. If the container does not quit on its own during the specified grace period, the command sends the SIGKILL signal and forcefully terminates the container.
  • The docker kill command immediately sends a SIGKILL signal to the container and interrupts the execution of its processes.

Note: Executing docker kill -s sigterm [container] produces the same effect as running docker stop [container] with default settings.

Always use docker stop to ensure that there are no errors when the container starts the next time. Use docker kill for unresponsive containers only.

Conclusion

This tutorial presented two commands to stop Docker containers, docker stop and docker kill. While docker stop gracefully shuts down container processes, docker kill can force quit a misbehaving container.

Containers are often stopped because they need to be replaced with an updated version. Read our article to learn how to update the Docker image and container.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
docker run Command: Syntax, Options, Examples
July 11, 2024

The docker run command lets you create and execute OCI-compatible containers using container images.
Read more
Docker Image vs. Container: The Major Differences
May 23, 2024

This article compares Docker images and containers, explains how they are related, and outlines their differences.
Read more
docker commit Explained
February 14, 2024

Docker images are immutable, i.e., their structure and contents cannot be altered after the build process finishes. However, if you want to make adjustments to a dockerized app...
Read more
docker save Command: How to Save Docker Image
February 29, 2024

While end-users download images from a container registry, developers and testers often need a quicker sharing method.
Read more