Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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...]
Code language: CSS (css)

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)
Code language: JavaScript (javascript)

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."
Code language: PHP (php)

This script stops specific containers and prints a success message.


7. Stop the Last Created Container

docker stop $(docker ps -lq)
Code language: JavaScript (javascript)

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
Code language: JavaScript (javascript)

9. Monitor and Stop Non-Responsive Containers

docker inspect --format='{{.State.Status}}' my_container
docker stop my_container
Code language: JavaScript (javascript)

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
Code language: JavaScript (javascript)

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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x