MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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 Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

How to Access a Running Docker Container Shell: docker exec vs docker attach

Need a shell inside a running container that is already in detached mode? This updated guide shows the correct workflow, compares docker exec and docker attach, and explains when SSH does and does not make sense.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More

Top 5 DevOps Tools To Learn Before Writing Your Dissertation

Writing a technical dissertation is rarely just an exercise in reading sources and formatting chapters. On paper, that sounds like the job. In reality, students in computer…

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