LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW

1. Overview

In a previous article, we learned how to remove a Docker image. However, a Docker image can only be removed if no Docker container is using that image. Hence, to remove a Docker image, it is necessary to remove all the Docker containers running with that image.

In this tutorial, we'll learn to remove Docker containers using different approaches.

2. Why Remove a Docker Container?

When a Docker container completes its execution, it attains the exited state. Such containers do not consume any CPU or memory, but they still use the machine's disk space. Also, stopped containers are not automatically removed unless we use the –rm flag while running the Docker container.

Hence, as more and more containers are moved into the exited state, the overall disk space consumed by them increases. As a result, we might not be able to launch new containers, or the Docker daemon would stop responding.

To avoid such scenarios, it is recommended to either run the Docker containers using the –rm flag or periodically remove the Docker containers manually.

Let's now learn how to remove Docker containers.

3. Remove a Single Docker Container

First, we'll start a CentOS Docker container in non-interactive mode. By doing so, the container will stop immediately after we run the container:

$ docker run --name mycontainer centos:7
$ docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS              NAMES
418c28b4b04e   centos:7      "/bin/bash"              6 seconds ago   Exited (0) 5 seconds ago                       mycontainer

Now let's remove the Docker container, mycontainer, using the docker rm command:

$ docker rm mycontainer
mycontainer
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

We can also use Docker container id instead of the Docker container name to remove the Docker container using the docker rm command:

$ docker rm 418c28b4b04e

4. Remove Multiple Docker Containers

We can also remove multiple Docker containers using the docker rm command. The docker rm command accepts a space-separated list of the Docker container name or id and removes all of them:

$ docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                      PORTS     NAMES
23c70ec6e724   centos:7   "/bin/bash"   6 seconds ago    Exited (0) 5 seconds ago              mycontainer3
fd0886458666   centos:7   "/bin/bash"   10 seconds ago   Exited (0) 9 seconds ago              mycontainer2
c223ec695e2d   centos:7   "/bin/bash"   14 seconds ago   Exited (0) 12 seconds ago             mycontainer1
$ docker rm c223ec695e2d mycontainer2 23c70ec6e724
c223ec695e2d
mycontainer2
23c70ec6e724

In the example above, there were three Docker containers in the exited state which we removed using the docker rm command.

We can use the Docker container name and id interchangeably with any Docker command. Notice that we used the Docker container id for the mycontainer1 and mycontainer3 and the container name for mycontainer2. 

5. Remove All the Docker Containers

Consider a scenario where too many stopped Docker containers are present on the machine, and now we wish to remove them all. Of course, we can use the above approach and pass all the containers id to the docker rm command. But let's look into a more optimized and simple command to remove all the Docker containers:

$ docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                      PORTS     NAMES
b5c45fa5764f   centos:7   "/bin/bash"   4 seconds ago    Exited (0) 3 seconds ago              mycontainer1
ed806b1743cd   centos:7   "/bin/bash"   9 seconds ago    Exited (0) 7 seconds ago              mycontainer2
2e00a052eb12   centos:7   "/bin/bash"   13 seconds ago   Exited (0) 12 seconds ago             mycontainer3
$ docker rm $(docker ps -qa)
b5c45fa5764f
ed806b1743cd
2e00a052eb12

The command docker ps -qa return the numeric ids of all the containers present on the machine. All these ids are then passed to the docker rm command, which will iteratively remove the Docker containers.

We can also use the docker container prune command to remove all the stopped containers:

$ docker container prune -f

Here, we use the -f flag to avoid the prompt for confirmation.

6. Forcefully Remove a Running Docker Container

All the commands that we discussed in the examples above works only if the Docker container is stopped. If we try to remove a running container without stopping it first, we'll get an error message similar to this:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f84692b27b0a        centos:7            "/bin/bash"         59 seconds ago      Up 58 seconds                           mycontainer
$ docker rm mycontainer
Error response from daemon:
  You cannot remove a running container f84692b27b0a18266f34b35c90dad655faa10bb0d9c85d73b22079dde506b8b5.
  Stop the container before attempting removal or force remove

One way to remove a running Docker container is first to stop that container using the docker stop command and then use the docker rm command to remove it.

Another way is to forcefully remove such containers using the -f option:

$ docker rm -f mycontainer
mycontainer

We can use the -f option to remove a single Docker container, multiple Docker containers, or all the Docker containers.

7. Conclusion

In this tutorial, we gained an understanding of why it is necessary to remove Docker containers. First, we learned to remove a container from a Linux machine. Further, we bulk removed the Docker containers using the docker rm and docker prune command.

Finally, we looked at how to remove Docker containers forcefully, which are in the running state.

LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments