List Docker Containers and Images

A common task when using Docker is to list the docker containers and docker images available on the system. I recently wrote about installing Docker on Fedora. This post will look at creating a new Docker container using a Fedora image – whist along the way looking at how to list docker containers and how to list docker images.

Searching and Downloading Docker Images

We can search for Fedora Docker images on the docker hub by running ‘docker search fedora’:

[root@localhost ~]# docker search fedora
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
fedora                           Official Fedora 21 base image and semi-off...   243       [OK]
fedora/couchdb                                                                   31                   [OK]
dockingbay/fedora-rust           Trusted build of Rust programming language...   2                    [OK]
vbatts/fedora-varnish            https://github.com/vbatts/laughing-octo/tr...   2                    [OK]
darksheer/fedora22               Base Fedora 22 Image -- Updated hourly          1                    [OK]
startx/fedora                                                                    1                    [OK]

The search will return a number of Docker images. The one we are interested in is the first in the list, which is the official Fedora 21 Docker image. To download the image, run:

[root@localhost ~]# docker pull fedora
Pulling repository fedora
c7d2f0130dae: Download complete
b0082ba983ef: Download complete
Status: Downloaded newer image for fedora:latest

List Docker Images

We can list the images on our system using the ‘docker images’ command. Running this now should display the newly downloaded Fedora image:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
fedora              23                  c7d2f0130dae        2 weeks ago         204.3 MB
fedora              latest              c7d2f0130dae        2 weeks ago         204.3 MB

Running the docker image ls command will generate the same output. Now, this is fine if we have a small number of images on our docker host. But what if we have a lot of docker images on the system? Then we can look at filtering the docker image list.

Format the Docker Image List Output

We can apply a custom format to the docker image list output. For example, if we just wanted to list the image repository and tag we could use:

$ docker images --format "table {{.Repository}}\t{{.Tag}}"

Applying a filter to the Docker Image List

The docker image list output can be filtered using the –filter flag. The basic syntax for this is:

$ docker images --filter "<key>=<value>"

The current available filters are:

  • dangling – find dangling images
  • label
  • before – filter images created before given id 
  • since – filter images created since given id 
  • reference – filter images whose reference matches the specified pattern

Using a filter to find Dangling Images

We can list dangling images by running the following:

$ docker images --filter "dangling=true"

Dangling images, are layers that have no relationship to any tagged images. We can use this filter to help automatically remove dangling images by running:

$ docker rmi -f $(docker images -f "dangling=true" -q) 

Using a filter to find an Image based on name

We can also use a filter to find docker images with a particular name. For example, to list Centos images we could use a filter such as:

$ docker images --filter "reference=cen*"

This will list all images on the docker host beginning with ‘cen’:

$ docker images --filter "reference=cen*"
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 831691599b88 9 days ago 215MB

Creating a Docker Container from a chosen Image

Now we have the Fedora image, we can create a docker container from it:

[root@localhost ~]# docker run -i -t fedora /bin/bash

The above command creates a new container using the Fedora image, and drops us into a bash shell within the container:

[root@localhost ~]# docker run -i -t fedora /bin/bash

Typing ‘exit’ in the shell will exit us from the container.

List Docker Containers

We can list the docker containers on the system by running:

[root@localhost ~]# docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
ff0eb34d2d94        fedora:23           "/bin/bash"         16 seconds ago       Exited (0) 15 seconds ago                           jovial_rosalind
3d3f8b8079bc        fedora:23           "/bin/bash"         54 seconds ago       Exited (0) 32 seconds ago                           suspicious_wright
58b4a78f5b63        fedora:23           "echo test"         About a minute ago   Exited (0) About a minute ago                       tender_curie
7b5e49ddee91        fedora:23           "/bin/bash"         31 minutes ago       Exited (0) 30 minutes ago                           stoic_wilson

The above command shows all Docker containers on the system.  The output of the command contains the following columns:

  • Container ID – A unique string that identifies each container.
  • Image – The Docker image used to create the container.
  • Command – The command that is executed when starting the container.
  • Created – The create time of the container.
  • Status – The current status of the container.
  • Ports – The container’s published ports.
  • Name – The friendly name of the container, by default this is automatically generated.

It’s possible to format the output of the command using the –format option. For example:

$ docker container ls --format 'table {{.containerid}}\t{{.image}}'
CONTAINER ID        IMAGE
ff0eb34d2d94        fedora:23

Docker List Running Containers

To list only running Docker containers you can use:

# docker ps

To list the most recently created Docker container, you can run:

[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
ff0eb34d2d94        fedora:23           "/bin/bash"         6 minutes ago       Exited (0) 6 minutes ago                       jovial_rosalind

Listing the Size of a Docker Container

Finally, to list the size of a Docker container, you can use the ‘-s‘ option:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES               SIZE
ff0eb34d2d94        fedora:23           "/bin/bash"         27 minutes ago      Exited (0) 27 minutes ago                       jovial_rosalind     0 B
3d3f8b8079bc        fedora:23           "/bin/bash"         27 minutes ago      Exited (0) 27 minutes ago                       suspicious_wright   5 B
58b4a78f5b63        fedora:23           "echo test"         28 minutes ago      Exited (0) 28 minutes ago                       tender_curie        0 B
7b5e49ddee91        fedora:23           "/bin/bash"         58 minutes ago      Exited (0) 57 minutes ago                       stoic_wilson        8 B

This adds an additional column displaying the size of each container.

Learning Docker?

If you are starting out, then I highly recommend this book.

Thirsty for more?

Then it’s time to take your Docker skills to the next level with this book (It’s my favorite).

Related posts

Docker Exec Command With Practical Examples

Debugging with Git Bisect

A Beginners Guide to Azure Repos

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More