1. Overview

Docker is a well-known platform that provides a consistent and efficient environment for users to develop, test, and deploy applications in containers. Docker containers are lightweight, standalone, and executable packages containing code, runtime, system tools, libraries, etc. to run a particular application or software.

While working on Docker, it’s important to keep the Docker environment organized for consistent development, testing, and deployment processes. For this purpose, we must stop and delete the running Docker containers when they’re no longer needed to free up system resources and ensure a streamlined workflow.

In this tutorial, we’ll learn how to stop and delete the running Docker containers.

2. View Running Containers

First, let’s check which containers are currently running on the system. We can list all the running Docker containers using the docker container list command:

$ docker container list
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS          PORTS      NAMES
d5e1395abb0c   nginx     "/docker-entrypoint.…"   24 hours ago   Up 3 minutes    80/tcp     nginx_Cont
37e8afba61e8   redis     "docker-entrypoint.s…"   24 hours ago   Up 14 minutes   6379/tcp   redis_Cont
1c36849daf98   ubuntu    "/bin/bash -c 'sleep…"   24 hours ago   Up 56 minutes              Cont1
495b6da20c9e   ubuntu    "/bin/bash -c 'sleep…"   24 hours ago   Up 6 minutes               ubuntu_Cont

Thus, we get the output that shows CONTAINER ID (unique identifier for each container), IMAGE (Docker image that the container is running from), COMMAND (command that was used to start the container), CREATED (date that shows how long ago the container was created), STATUS (indicates the current status of the container), PORT (shows the port mappings for the container) and NAMES (name given to the container) of the currently running container.

Alternatively, we can also use the docker ps to view and display the running Docker containers:

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS          PORTS      NAMES
d5e1395abb0c   nginx     "/docker-entrypoint.…"   24 hours ago   Up 3 minutes    80/tcp     nginx_Cont
37e8afba61e8   redis     "docker-entrypoint.s…"   24 hours ago   Up 14 minutes   6379/tcp   redis_Cont
1c36849daf98   ubuntu    "/bin/bash -c 'sleep…"   24 hours ago   Up 56 minutes              Cont1
495b6da20c9e   ubuntu    "/bin/bash -c 'sleep…"   24 hours ago   Up 6 minutes               ubuntu_Cont

The output indicates that nginx_Cont has been running for 3 minutes, redis_Cont has been running for 14 minutes, and so on.

3. Stop Running Docker Container

We can stop any particular Docker container, multiple containers, or even all the Docker containers using the docker stop command.

3.1. Stop a Single Docker Container

Let’s stop a single Docker container by specifying its ID along with the docker stop command. For instance, we stop the docker container whose ID is 37e8afba61e8:

$ docker stop 37e8afba61e8 
37e8afba61e8

Moreover, we can also use the specific container’s name i.e., redis_Cont with the docker stop command to stop it:

$ docker stop redis_Cont   
redis_Cont

Subsequently, the specified single container has stopped.

3.2. Stop Multiple Docker Containers

We can stop multiple Docker containers by specifying their container IDs or names with the docker stop command. Let’s stop the Docker container whose IDs are 1c36849daf98, d5e1395abb0c, and 495b6da20c9e:

$ docker stop 1c36849daf98 d5e1395abb0c 495b6da20c9e
1c36849daf98
d5e1395abb0c
495b6da20c9e

Hence, each specified container has stopped in sequence.

3.3. Stop All Docker Container

To stop all the running containers at once, use the $(docker ps -aq) command to fetch their IDs and pass them to the docker stop command:

$ docker stop $(docker ps -aq)
d5e1395abb0c
37e8afba61e8
1c36849daf98
495b6da20c9e

By doing so, all the running containers have stopped.

4. Delete Docker Container

After stopping the Docker containers, we can delete them from the system. We can delete or remove a single, multiple, or all Docker containers per the requirement with the help of the docker rm command.

4.1. Delete a Single Docker Container

Let’s delete a single container using the docker rm command by specifying its ID i.e., d5e1395abb0c:

$ docker rm d5e1395abb0c
d5e1395abb0c

We can also specify the required container’s name i.e., redis_Cont with the docker rm command to delete it:

$ docker rm redis_Cont  
redis_Cont

4.2. Delete Multiple Docker Containers

We can delete multiple Docker containers by specifying their IDs or names with the docker rm command. Here, we remove containers with IDs 495b6da20c9e, 37e8afba61e8, and 1c36849daf98:

$ docker rm 495b6da20c9e 37e8afba61e8 1c36849daf98
495b6da20c9e
37e8afba61e8
1c36849daf98

As a result, each specified container has been removed.

4.3. Delete All Docker Containers

To delete or remove all the Docker containers from the system, we can fetch their IDs using the $(docker ps -aq) command and pass them to the docker rm command:

$ docker rm $(docker ps -aq)
2f803cde883f
db801fa0ebce

Therefore, all the Docker containers have been deleted from the system.

5. Stop and Delete Docker Container with One Command

For a more efficient approach, we can stop and delete Docker containers with a single command via the docker rm -f command. This command can be used to forcefully stop and remove a single or all the Docker containers in one step.

Let’s use the docker rm -f command along with the specific container’s name or ID to stop and delete it immediately. Here, we forcefully stop and delete the test_Cont container:

$ docker rm -f test_Cont
test_Cont

Now, we stop and delete all the docker containers simultaneously using the $(docker ps -q) command with the docker rm -f command:

$ docker rm -f $(docker ps -q)
1cfcef7b368d
8839b7fee01d
907ccc7f9897

Thus, we’ve successfully stopped and deleted all the running containers from the system.

6. Conclusion

In this article, we learned different ways to stop and delete the running Docker containers. Particularly, we can use the docker stop command with the desired container’s ID or name to stop it. After that, we can delete the Docker container via the docker rm command.

Moreover, we can use the single docker rm -f $(docker ps -q) command to stop and delete all running Docker containers.

Ultimately, we can stop and delete a single, multiple, or all Docker containers using any of these effective methods based on the specific requirement.