Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker stop with examples

Here’s a complete tutorial on docker stop, including its purpose, how to use it, examples, and use cases.


What is docker stop?

docker stop is a Docker command used to gracefully stop a running container by sending the SIGTERM signal. This allows the main process inside the container to perform necessary cleanup before shutting down. If the container doesn’t stop within the specified timeout (default is 10 seconds), Docker sends a SIGKILL signal to forcefully stop it.

Key Features:

  • Stops running containers gracefully (tries to avoid data loss).
  • Allows you to specify a timeout before forcing the stop.
  • Can stop multiple containers at once.
  • Useful for service management, maintenance, and resource control.

Basic Syntax

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

Common Options:

  • -t, --time: Specify the timeout (in seconds) before forcefully stopping the container. The default is 10 seconds.

Examples of docker stop

1. Stop a Running Container

docker stop my_container

This sends a SIGTERM to the container named my_container and waits up to 10 seconds before forcefully stopping it.


2. Stop a Container by ID

docker stop a1b2c3d4e5f6

You can stop a container using its unique container ID.


3. Stop Multiple Containers

docker stop container1 container2 container3

This command stops multiple containers simultaneously.


4. Stop All Running Containers

docker stop $(docker ps -q)

This stops all currently running containers.


5. Specify a Custom Timeout

docker stop -t 20 my_container

This gives the container 20 seconds to stop gracefully before sending SIGKILL.


6. Use in a Shell Script

#!/bin/bash
docker stop web_container db_container cache_container
echo "Containers stopped successfully."

This script stops specific containers and prints a success message.


7. Stop the Last Created Container

docker stop $(docker ps -lq)

This stops the most recently created container.


8. Stop Containers with Specific Filters

Stop all containers that were created from a specific image:

docker ps -q --filter "ancestor=nginx" | xargs docker stop

9. Monitor and Stop Non-Responsive Containers

docker inspect --format='{{.State.Status}}' my_container
docker stop my_container

Check the container status before stopping it.


10. Automate Maintenance with docker stop

#!/bin/bash
for container in $(docker ps -q); do
  docker stop $container
done

This script stops all running containers one by one.


Use Cases for docker stop

1. Graceful Service Shutdown

  • Safely stop services to apply updates or perform maintenance.
  • Ensure that services have time to clean up resources or write data before shutting down.

2. Resource Management

  • Free up system resources by stopping non-critical containers during high-demand periods.

3. Debugging and Troubleshooting

  • Stop unresponsive or malfunctioning containers for investigation.
  • Combine with docker logs and docker inspect to identify issues.

4. Scheduled Maintenance

  • Use docker stop in scheduled scripts to stop services during off-peak hours.

5. CI/CD Pipelines

  • Stop containers after automated tests to ensure a clean environment for the next test cycle.

6. Cleanup and Recovery

  • Stop and restart containers during service recovery or when replacing outdated services.

List of Common docker stop Commands

CommandDescription
docker stop my_containerGracefully stop a running container
docker stop container1 container2Stop multiple containers
docker stop a1b2c3d4e5f6Stop a container using its ID
docker stop $(docker ps -q)Stop all running containers
docker stop -t 20 my_containerStop a container with a 20-second timeout
docker stop $(docker ps -lq)Stop the last created container
`docker ps -q –filter “ancestor=nginx”xargs docker stop`

Best Practices for Using docker stop:

  1. Use a reasonable timeout (-t) to allow services to shut down cleanly.
  2. Monitor container logs after stopping to verify that the service shut down properly.
  3. Combine with docker ps and filters to target specific containers for stopping.
  4. Avoid using -t 0 unless necessary—give services at least a few seconds to clean up.
  5. Automate container shutdowns for scheduled maintenance or batch processing.

Common Errors and Solutions

  1. “No such container”
    → Ensure the container exists and is running. Check with docker ps.
  2. “Container failed to stop within the timeout”
    → Increase the timeout (-t) or use docker kill to forcefully stop the container.
  3. “Permission denied”
    → Ensure you have the necessary permissions or run with sudo.
  4. Service Does Not Restart Properly
    → Check container logs and configuration before restarting.

Combining docker stop with Other Commands

Stop and Remove a Container

docker stop my_container && docker rm my_container

Stop, Inspect, and Restart

docker stop my_app
docker inspect my_app
docker start my_app

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x