The docker stop command is essential for managing Docker containers, allowing you to gracefully stop running containers. This guide will explain the docker stop command in detail, including various options and practical examples to help you manage your Docker containers effectively.

What is the docker stop Command?

The docker stop command stops one or more running containers by sending a SIGTERM signal followed by a SIGKILL after a grace period. This ensures that the container is given a chance to clean up and shut down gracefully.

Basic Syntax

The basic syntax for the docker stop command is:

docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stopping a Container

To stop a running container, use the following command:

docker stop <container_id>

Replace <container_id> with the ID or name of the container you want to stop. For example:

docker stop my_nginx

Stopping Multiple Containers

To stop multiple containers at once, list the container IDs or names separated by spaces:

docker stop container1 container2 container3

Specifying a Timeout

By default, Docker waits 10 seconds for a container to stop before killing it. You can specify a different timeout in seconds using the -t option:

docker stop -t 5 my_nginx

This command sets a 5-second timeout before forcefully stopping the container.

Conclusion

The docker stop command is a crucial tool for managing the lifecycle of your Docker containers. By using this command, you can ensure that your containers are stopped gracefully, allowing them to perform necessary cleanup tasks. Whether you are stopping a single container or multiple containers, the docker stop command provides the flexibility and control you need.