DevOps

Docker Remove Container Example

This example will show how to remove Docker containers, that is, instances of images. For this, we will be using the rm command, along with other options, for making the removal more flexible.

For this example, Linux Mint 18 and Docker version 1.12.1 have been used.
 
 
 
 
 
 

Tip
You may skip Docker installation and jump directly to the beginning of the example below.

1. Installation

Note: Docker requires a 64-bit system with a kernel version equal or higher to 3.10.

We can install Docker simply via apt-get, without the need of adding any repository, just installing the docker.io package:

sudo apt-get update
sudo apt-get install docker.io

For more details, you can follow the Install Docker on Ubuntu Tutorial.

2. Setting up some containers

2.1. Pulling a sample image

As you should know, before creating containers, we need Docker images.

The easiest way to obtain a Docker image is to pull one from the Docker Hub. You don’t have to have an account for pulling images.

Let’s pull a Busybox image:

docker pull busybox

2.2. Creating some containers

Let’s create some containers:

docker run busybox
docker run --name=busybox2 busybox
docker run --name=busybox3 -i -t -d busybox

Note: the difference between the last container with the previous two, is that we keep it alive (achieved with -i and -t, and then detached with  -d).

3. Removing containers

Removing Docker containers is so easy:

docker rm <container-name|container-id>

First, let’s list our containers with:

docker rm <container-id|container-name>

Which, for this case, the output is:

CONTAINER ID        IMAGE           COMMAND       CREATED                  STATUS                       NAMES
d80296418f95        busybox         "sh"          About a minute ago       Up 3 seconds                 busybox3
df2424cd3d10        busybox         "sh"          About a minute ago       Exited (0) 4 seconds ago     busybox2
1ba6b04dbddf        busybox         "sh"          About a minute ago       Exited (0) 4 seconds ago     furious_cray

As we said, we can delete the containers by the id:

docker rm 1ba6b04dbddf

Or, also, by the name:

docker rm busybox2

If we try to remove the remaining container:

docker rm busybox3

Docker will throw an error:

Error response from daemon: You cannot remove a running container d80296418f95634e7cd8e90870d8592e73e014a9cd7d455a1683de6850205f0d. Stop the container before attempting removal or use -f

The message is clear enough: we cannot delete a running container, at least as the other containers. We can stop it and then delete it:

docker stop busybox3
docker rm busybox3

Or, force the removal with the -f (--force) option:

docker rm -f busybox3

3.1. Remove all containers

Some times, we may want to delete all the containers, and doing it one by one can take a while, if we have quite many. For this, first, we have to use the ps command in the following way:

docker ps -a -q

The -a option is for listing all the containers, as you already probably know; and the -q,  is for “quite” mode, i.e., to show only the container ids. For the containers we created before, the output would be just the following:

d80296418f95
df2424cd3d10
1ba6b04dbddf

Knowing this, it’s all about running this command in a subshell, for docker rm:

docker rm $(docker ps -a -q)

Of course, we would get the same error as in the previous chapter if we execute that command with a running container, we will get an error.

If we are completely sure of forcing the removal, we can use the -q option:

docker rm -f $(docker ps -a -q)

Or, if we prefer it, first stop all the containers and then remove them:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

3.2. Remove containers using filters

We have also available the option of removing containers using the filter option for the ps command, -f (--filter).

The format would be the following:

docker rm $(docker ps -a -q -f <pattern>)

For example, for deleting containers that include the string busybox in their names:

docker rm $(docker ps -a -q -f "name=busybox")

4. Summary

This tutorial has shown how to delete Docker containers, starting from the very basic way, and then seeing other interesting options, such as removing several containers at the same time, or using filters.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button