How to Remove Docker Containers

Containers are awesome when it comes to testing and deploying a project. But containers can quickly eat disk space if you create multiple containers.

In this Docker beginner tutorial, I’ll show you how to remove docker containers.

In the simplest form, you can remove a docker container with the docker rm command:

docker rm container_id_or_name

If you want to remove all containers, stop the running ones first and then remove them:

docker ps -q | xargs docker stop
docker ps -q | xargs docker rm

But you won’t always have a simple life with containers. And this is why I am going to show various scenarios in which you can delete docker containers.

Practical examples of removing docker containers

As you can guess, to remove a container, you need to know its name or ID. You can check all the docker containers on your system (both stopped and running) with the docker ps command like this:

abhishek@nuc:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
07e97581c5d8        debian              "bash"              9 minutes ago       Up 9 minutes                                    container2
6ef4545eef37        ubuntu              "bash"              11 minutes ago      Up 11 minutes                                   container1
707e40ce3c5a        ubuntu              "/bin/bash"         15 minutes ago      Exited (0) 15 minutes ago                       boring_payne
8047ab8e3673        ubuntu              "/bin/bash"         34 minutes ago      Exited (0) 15 minutes ago                       relaxed_wiles
ce84231ab213        debian              "bash"              42 minutes ago      Exited (0) 42 minutes ago                       bold_golick
12a18eaa291b        hello-world         "/hello"            2 days ago          Exited (0) 2 days ago                           brave_mendeleev

The first column gives container ID and the last column gives container name. You can also notice that the running docker containers have ‘Up’ in the Status column.

Now that you know how to get the container ID and name, let’s see how to remove it:

Remove docker container

This is the simplest. You use the container ID or name in this fashion:

docker rm container_id_or_name

You’ll not get a message like container removed in the output. It just displays the container id or name that your provided.

abhishek@nuc:~$ docker rm 12a18eaa291b
12a18eaa291b

Remove a running container

If a container is running and you try to remove it, you’ll see an error like this:

abhishek@nuc:~$ docker rm container1
Error response from daemon: You cannot remove a running container 6ef4545eef378788e5e9d7ac1cf2e0a717480608adb432be99fd9b3d3a604c12. Stop the container before attempting removal or force remove

Quite clearly, you should stop the docker container first and then remove it:

docker stop container_id_or_name
docker rm container_id_or_name

Docker gives you the -f option to force remove a container. With this, you can remove a running container:

docker rm -f container_id_or_name

This is not recommended because it sends kill command and your container might not save its state.

Remove multiple docker containers

You can specify more than one docker containers while removing:

docker rm container1 container2 container3

Remove multiple docker containers associated with a particular docker image

In the example, let’s say you want to remove all the containers associated with docker image ubuntu.

I advise you to stop the containers first:

docker ps -a -q --filter ancestor=ubuntu | xargs docker stop

And then remove these containers:

docker ps -a -q --filter ancestor=ubuntu | xargs docker rm

To explain the command above, the docker ps command output was filtered with containers associated with ubuntu image and then the -q option only give the containers ID. Combine it with xargs command to feed the container IDs (associated to Ubuntu) to docker rm command.

Remove all stopped containers

If you want to remove all the stopped containers, you can filter them by their status and then stop them in this fashion:

docker ps -a -q -f status=exited | xargs docker rm

Remove all Docker containers

If you want to remove all containers from your system, you should make sure to stop all containers first and then remove them:

docker ps -q | xargs docker stop
docker ps -q | xargs docker rm

I hope you liked this Docker tutorial. Stay tuned for more.