How to Stop a Docker Container?

How to Stop a Docker Container: A Step-by-Step Guide by OpsNexa

Docker containers provide an isolated environment for running applications, making them an essential tool for modern software development and deployment. However, as with any resource-consuming system, you may occasionally need to stop a Docker container to manage system resources, troubleshoot issues, or simply halt an application. Stopping containers correctly ensures that they do not consume unnecessary resources or impact system performance.

In this guide by OpsNexa, we will explain how to stop Docker containers in various scenarios, using simple commands to control and manage them effectively.

What is a Docker Container?

Before we dive into the steps for stopping a Docker container, let’s briefly review what a Docker container is.

A Docker container is a lightweight, portable, and self-sufficient unit that runs applications in isolation from the host system. Containers can be started, stopped, and removed with ease, and they are built from Docker images that define the application and its environment.

Unlike virtual machines, which require an entire operating system to run, Docker containers share the host system’s kernel, making them much more efficient and lightweight.

However, when you no longer need a running container or wish to stop a container temporarily, it’s important to use the correct commands to stop it without losing any data or impacting other containers.

Step 1: Identify Running Containers

Before stopping any containers, you first need to identify which containers are currently running on your system.

To list all running containers, use the following command:

bash
docker ps

Explanation:

  • docker ps: Lists all active containers. This command provides essential information, including the container ID, image name, status, and port mappings.

If you want to see all containers, including those that are stopped, use:

bash
docker ps -a

This command will show both running and stopped containers, allowing you to manage all containers on your system.

Step 2: Stop a Docker Container

Stopping a Docker container is as simple as using the docker stop command. The basic syntax is:

bash
docker stop <container_id or container_name>

For example, if your container ID is e3f5a7d0a6b7, you would stop the container with:

bash
docker stop e3f5a7d0a6b7

Alternatively, if you know the container name (e.g., my_container), you can use the name instead of the container ID:

bash
docker stop my_container

Explanation:

  • docker stop: This command is used to stop a running container.

  • <container_id or container_name>: The unique identifier or name of the container you wish to stop. You can obtain this information by running docker ps.

By default, Docker gives the container a grace period (10 seconds) to stop before forcibly terminating it. This allows the container to cleanly shut down any running processes.

Step 3: Forcefully Stop a Docker Container

In some situations, a container might not stop gracefully within the default timeout period. If that happens, you can forcefully stop the container using the -f (force) flag.

bash
docker stop -f <container_id or container_name>

This command immediately terminates the container, regardless of whether it has finished its current operations. Use this option with caution, as it can result in data loss or corruption if the container is in the middle of writing data.

Step 4: Stopping Multiple Docker Containers

If you need to stop multiple containers at once, you can do so by providing a list of container IDs or names separated by spaces.

bash
docker stop <container_id_1> <container_id_2> <container_name_3>

For example, to stop containers with IDs e3f5a7d0a6b7 and d8b2fbb8c3a0, you would run:

bash
docker stop e3f5a7d0a6b7 d8b2fbb8c3a0

Alternatively, you can use Docker’s container names if you know them:

bash
docker stop web_app db_service

This approach makes it easy to manage and stop multiple containers with a single command.

Step 5: Verify the Container Has Stopped

After stopping a container, it’s important to verify that it has been successfully stopped. To do this, you can list all active containers again by running:

bash
docker ps

If the container no longer appears in the list, it has been stopped. If you want to see all containers (including those that are stopped), you can use:

bash
docker ps -a

The container will now show a status of Exited, indicating that it is no longer running.

Step 6: Stopping All Running Containers

If you need to stop all running containers at once, you can use a combination of the docker ps and docker stop commands with a simple shell script. Run the following command:

bash
docker stop $(docker ps -q)

Explanation:

  • docker ps -q: This command returns the IDs of all running containers, and $(...) is used to pass these IDs as arguments to the docker stop command.

This command stops all active containers in one go. It’s particularly useful when you’re working with multiple containers and need to stop them all without having to manually specify each container ID or name.

Step 7: Stopping a Docker Container with Docker Compose

If you are using Docker Compose to manage multiple containers for a multi-service application, stopping the containers is just as easy. In the directory containing your docker-compose.yml file, use the following command:

bash
docker-compose stop

This command stops all containers defined in your docker-compose.yml file, regardless of their individual names or IDs.

To stop specific services defined in the docker-compose.yml file, specify the service name:

bash
docker-compose stop <service_name>

For example, to stop a service named web, run:

bash
docker-compose stop web

This command ensures that the specified service(s) are stopped without affecting the other services in your application.

Step 8: Stopping Docker Containers Automatically on System Shutdown

In some scenarios, you may want Docker containers to stop automatically when your system shuts down. To achieve this, you can set the restart policy for your containers.

You can configure a container to stop automatically when Docker shuts down using the --restart=always flag when starting the container. However, this flag applies when starting the container, not stopping it. If you’re looking for automatic shutdown behavior on system restart or shutdown, Docker’s default behavior is to not automatically stop containers.

To stop the containers manually before system shutdown, you can create a system shutdown hook or script to invoke the docker stop command.

Step 9: Clean Up Stopped Containers

If you have stopped containers that are no longer needed, you may want to remove them to free up disk space. You can remove a stopped container using the following command:

bash
docker rm <container_id or container_name>

To remove all stopped containers, use:

bash
docker container prune

This will remove all containers that are not currently running. Always double-check that you no longer need these containers before removing them, as this action cannot be undone.

Conclusion

Knowing how to stop Docker containers is a fundamental skill for anyone working with Docker. Whether you’re temporarily halting a service, cleaning up resources, or troubleshooting issues, the ability to stop Docker containers efficiently is essential for maintaining a smooth and well-functioning Docker environment.

In this guide by OpsNexa, we’ve covered multiple ways to stop a Docker container, including forcefully stopping it, stopping multiple containers, and using Docker Compose for managing containers in a multi-service environment. By following these steps, you’ll be able to manage your Docker containers more effectively and keep your system optimized.

Happy Dockerizing!