• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • Docker is a well-known platform that allows you to manage run and ship your applications as containers that package the OS and dependent libraries along with your application together.

    We’ll be covering managing Docker images, mainly briefing about the ways to list Docker images and get the required information and then building upon that learning to remove one or more images in efficient way.

    So let’s get started.

    Listing Docker Images

    To list pulled Docker images, use:

    [email protected]:~$ docker image ls
    REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
    redis         latest    aa4d65e670d6   9 days ago      105MB
    mysql         latest    c60d96bd2b77   10 days ago     514MB
    ubuntu        latest    c29284518f49   2 weeks ago     72.8MB
    nginx         latest    4cdc5dd7eaad   3 weeks ago     133MB
    hello-world   latest    d1165f221234   4 months ago    13.3kB
    redis         4.0       191c4017dcdd   15 months ago   89.3MB
    [email protected]:~$

    As with other docker commands, it supports images command with multiple options.

     $ docker images [OPTIONS] [REPOSITORY[:TAG]]

    If you run docker images without any options, it’ll show you the top-level images like docker image ls, their repository and tags and their size on disk.

    [email protected]:~$ docker images
    REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
    mysql         latest    c60d96bd2b77   10 days ago     514MB
    ubuntu        latest    c29284518f49   2 weeks ago     72.8MB
    nginx         latest    4cdc5dd7eaad   3 weeks ago     133MB
    hello-world   latest    d1165f221234   4 months ago    13.3kB
    redis         4.0       191c4017dcdd   15 months ago   89.3MB
    [email protected]:~$

    These images have intermediate layers that increase usability, speed up the build process, and reduces disk usage which isn’t shown by the above command. The SIZE is the combined space taken up by the image and all its parent images. If you save the contents of the image as a Tar file when you docker save an image, it’ll equal this listed size.

    An image gets listed multiple times if it has multiple repository names or tags though the single image identified by its IMAGE ID uses up the SIZE listed only once.

    List most recent images

    [email protected]:~$ docker images
    REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
    mysql         latest    c60d96bd2b77   10 days ago     514MB
    ubuntu        latest    c29284518f49   2 weeks ago     72.8MB
    nginx         latest    4cdc5dd7eaad   3 weeks ago     133MB
    hello-world   latest    d1165f221234   4 months ago    13.3kB
    redis         4.0       191c4017dcdd   15 months ago   89.3MB
    [email protected]:~$

    List images by Repository and Tag

    To list all images that belong to a particular repository, specify the repository name like:

    [email protected]:~$ docker images redis
    REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    redis        latest    aa4d65e670d6   9 days ago      105MB
    redis        4.0       191c4017dcdd   15 months ago   89.3MB
    [email protected]:~$

    You can additionally specify the repository name with a tag for a more filtered output:

    [email protected]:~$ docker images redis:4.0
    REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    redis        4.0       191c4017dcdd   15 months ago   89.3MB
    [email protected]:~$

    List full-length image IDs

    To list all images without truncating the IMAGE ID, use:

    [email protected]:~$ docker images --no-trunc
    REPOSITORY    TAG       IMAGE ID                                                                  CREATED         SIZE
    redis         latest    sha256:aa4d65e670d6518e5da96ca9d1a76370a942970a8802e6d5cc6bcf058ab12ca7   9 days ago      105MB
    mysql         latest    sha256:c60d96bd2b771a8e3cae776e02e55ae914a6641139d963defeb3c93388f61707   10 days ago     514MB
    ubuntu        latest    sha256:c29284518f497b8c5f49933e74e43ca5221e69c8251e780427f7d12f716625ff   2 weeks ago     72.8MB
    nginx         latest    sha256:4cdc5dd7eaadff5080649e8d0014f2f8d36d4ddf2eff2fdf577dd13da85c5d2f   3 weeks ago     133MB
    hello-world   latest    sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726   4 months ago    13.3kB
    redis         4.0       sha256:191c4017dcdd3370f871a4c6e7e1d55c7d9abed2bebf3005fb3e7d12161262b8   15 months ago   89.3MB
    [email protected]:~$

    List Image Digests

    Docker images that use v2 or later format have a content-addressable identifier known as a digest. To list image digest values, use --digest flag as:

    [email protected]:~$ docker images --digests
    REPOSITORY    TAG       DIGEST                                                                    IMAGE ID       CREATED         SIZE
    redis         latest    sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59   aa4d65e670d6   9 days ago      105MB
    mysql         latest    sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd   c60d96bd2b77   10 days ago     514MB
    ubuntu        latest    sha256:b3e2e47d016c08b3396b5ebe06ab0b711c34e7f37b98c9d37abe794b71cea0a2   c29284518f49   2 weeks ago     72.8MB
    nginx         latest    sha256:c5aab9d8e259d54af91e0548abf1fa8188a43079eb86b6ba8df9f482a5380720   4cdc5dd7eaad   3 weeks ago     133MB
    hello-world   latest    sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e   d1165f221234   4 months ago    13.3kB
    redis         4.0       sha256:2e03fdd159f4a08d2165ca1c92adde438ae4e3e6b0f74322ce013a78ee81c88d   191c4017dcdd   15 months ago   89.3MB
    [email protected]:~$

    With a 2.0 registry, you can use these digests with push, pull, create, run and rmi commands. This also works with FROM command in a Dockerfile.

    Filtering the Output

    docker command supports filtering with images by using --filter flag. The currently supported filters are:

    • dangling (boolean – true or false)
    • label (label=<key> or label=<key>=<value>)
    • before (<image-name>[:<tag>]<image id> or <[email protected]>) – filter images created before given id or references
    • since (<image-name>[:<tag>]<image id> or <[email protected]>) – filter images created since given id or references
    • reference (pattern of an image reference) – filter images whose reference matches the specified pattern

    Further, you can use multiple filters by combining them like:

    $ docker images --filter "<key1>=<value1>" --filter"<key2>=<value2>"

    Example:

    [email protected]:~$ docker images --filter "before=redis" --filter "since=hello-world"
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    mysql        latest    c60d96bd2b77   10 days ago   514MB
    ubuntu       latest    c29284518f49   2 weeks ago   72.8MB
    nginx        latest    4cdc5dd7eaad   3 weeks ago   133MB
    [email protected]:~$

    Formatting the Output

    docker images support formatting output that may be needed for nesting with other commands, scripting, or otherwise. These are the supported placeholders for the --format flag:

    Placeholder Description
    .ID Image ID
    .Repository Image repository
    .Tag Image tag
    .Digest Image digest
    .CreatedSince Elapsed time since the image was created
    .CreatedAt The time when the image was created
    .Size Image disk size

    As an example, the below command prints the output without headers and outputs the ID and Repository separated by colon ( : ) for all images:

    [email protected]:~$ docker images --format "{{.ID}}: {{.Repository}}"
    aa4d65e670d6: redis
    c60d96bd2b77: mysql
    c29284518f49: ubuntu
    4cdc5dd7eaad: nginx
    d1165f221234: hello-world
    191c4017dcdd: redis
    [email protected]:~$

    Or to list all images with their repository and tag in a table format, you can use:

    [email protected]:~$ docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
    IMAGE ID       REPOSITORY    TAG
    aa4d65e670d6   redis         latest
    c60d96bd2b77   mysql         latest
    c29284518f49   ubuntu        latest
    4cdc5dd7eaad   nginx         latest
    d1165f221234   hello-world   latest
    191c4017dcdd   redis         4.0
    [email protected]:~$

    Removing Docker Images

    To remove one or more Docker images from the system, we use:

    $ docker image rm [OPTIONS] IMAGE [IMAGE...]

    To remove a single image, simply specify the image name:

    [email protected]:~$ docker image rm redis
    Untagged: redis:latest
    Untagged: [email protected]:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59
    Deleted: sha256:aa4d65e670d6518e5da96ca9d1a76370a942970a8802e6d5cc6bcf058ab12ca7
    Deleted: sha256:3bd00d38f5ca70200050477c527cc60cfdf82911d6fe03932e2bcae31a95cfa2
    Deleted: sha256:22722fde392d188cfbe5bbd0c2451cc71cf5b000afc0e5114c1066bb5e113ec9
    Deleted: sha256:38212b55ef525e86cd726cd83c1a82a6009c68d24771d6e93d439fdc88e66f0e
    Deleted: sha256:188c498579cef37b65a93d6448c6b129fa07d5740fc213a18843ff22d80cd10d
    Deleted: sha256:2117165cd53c98f13ec7af36c9d8acd239fc541c847efaccb49885decf615d68
    [email protected]:~$

    Or to remove multiple images, specify multiple image names separated by space:

    [email protected]:~$ docker image rm redis mariadb
    Untagged: redis:latest
    Untagged: [email protected]:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59
    Deleted: sha256:aa4d65e670d6518e5da96ca9d1a76370a942970a8802e6d5cc6bcf058ab12ca7
    Deleted: sha256:3bd00d38f5ca70200050477c527cc60cfdf82911d6fe03932e2bcae31a95cfa2
    Deleted: sha256:22722fde392d188cfbe5bbd0c2451cc71cf5b000afc0e5114c1066bb5e113ec9
    Deleted: sha256:38212b55ef525e86cd726cd83c1a82a6009c68d24771d6e93d439fdc88e66f0e
    Deleted: sha256:188c498579cef37b65a93d6448c6b129fa07d5740fc213a18843ff22d80cd10d
    Deleted: sha256:2117165cd53c98f13ec7af36c9d8acd239fc541c847efaccb49885decf615d68
    Untagged: mariadb:latest
    Untagged: [email protected]:3b6f9fa1d406e168998d62501b2ee4f27d53138bebfcdac03540758996c5ff1d
    Deleted: sha256:fd17f57768027456cc17987058474fb21d3c51e9dd764e4497c1dfe92ff058db
    Deleted: sha256:a638f04e531b032c81a84bda59a36d1df3c4cec62560c403bc2edb642bce79ba
    Deleted: sha256:af7a7e7bf72e7ad0c5227995f219d9094fafbe0ac973dbe7eb4ab190a5a58ba5
    Deleted: sha256:a0cfd81b291e8da6bca0731c9db70fe1b61d176906b1cf05ade1cd61572ffaaa
    Deleted: sha256:38b00cb5dab64398092fab2b18563af1ef4a9445dec8a531ac725059dc218f41
    Deleted: sha256:6e60d29d2d76125f989f503a78729984f7ba3e26bfbf7038d5ab644b8755b7c2
    Deleted: sha256:1dafdd60e471b4c1f5244cfe1d20d79f934f504ef42180b637886be2b0b74370
    Deleted: sha256:2fffe8e736cdb99359084e297f4bb54a4cac879366bd3e4333d3dbe966f8e9a6
    Deleted: sha256:f728037697805db453111266541c202c74971484ef359ea29b9c60064ed9e47e
    Deleted: sha256:28155a13db3520201db576bf6d56b68fc08ef27b1c4c49dbfeadef523d35c5f0
    Deleted: sha256:7555a8182c42c7737a384cfe03a3c7329f646a3bf389c4bcd75379fc85e6c144
    [email protected]:~$

    Force Removal

    There may be containers running that are using the images which you’re trying to delete. In such cases, Docker will issue you a warning when you try to delete a referenced image.

    [email protected]:~$ docker image rm nginx
    Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container ce908eadf829 is using its referenced image 4cdc5dd7eaad
    [email protected]:~$

    You can stop the associated container first and then retry or else use -f flag which forces the image removal (be careful).

    [email protected]:~$ docker image rm nginx -f
    Untagged: nginx:latest
    Untagged: [email protected]:c5aab9d8e259d54af91e0548abf1fa8188a43079eb86b6ba8df9f482a5380720
    Deleted: sha256:4cdc5dd7eaadff5080649e8d0014f2f8d36d4ddf2eff2fdf577dd13da85c5d2f
    [email protected]:~$

    Prune Images

    There are certain situations where unused images are consuming disk space or you just need a cleanup of old dangling images. You can clean such unused images by using:

    $ docker image prune

    Example:

    [email protected]:~$ docker image prune
    WARNING! This will remove all dangling images.
    Are you sure you want to continue? [y/N] y
    Total reclaimed space: 0B
    [email protected]:~$

    If you wish to clean up all images which aren’t used by any containers as well, use -a flag:

    [email protected]:~$ docker image prune -a
    WARNING! This will remove all images without at least one container associated to them.
    Are you sure you want to continue? [y/N] y
    Deleted Images:
    untagged: mariadb:latest
    untagged: [email protected]:3b6f9fa1d406e168998d62501b2ee4f27d53138bebfcdac03540758996c5ff1d
    deleted: sha256:fd17f57768027456cc17987058474fb21d3c51e9dd764e4497c1dfe92ff058db
    deleted: sha256:a638f04e531b032c81a84bda59a36d1df3c4cec62560c403bc2edb642bce79ba
    deleted: sha256:af7a7e7bf72e7ad0c5227995f219d9094fafbe0ac973dbe7eb4ab190a5a58ba5
    deleted: sha256:a0cfd81b291e8da6bca0731c9db70fe1b61d176906b1cf05ade1cd61572ffaaa
    deleted: sha256:38b00cb5dab64398092fab2b18563af1ef4a9445dec8a531ac725059dc218f41
    deleted: sha256:6e60d29d2d76125f989f503a78729984f7ba3e26bfbf7038d5ab644b8755b7c2
    deleted: sha256:1dafdd60e471b4c1f5244cfe1d20d79f934f504ef42180b637886be2b0b74370
    deleted: sha256:2fffe8e736cdb99359084e297f4bb54a4cac879366bd3e4333d3dbe966f8e9a6
    deleted: sha256:f728037697805db453111266541c202c74971484ef359ea29b9c60064ed9e47e
    deleted: sha256:28155a13db3520201db576bf6d56b68fc08ef27b1c4c49dbfeadef523d35c5f0
    deleted: sha256:7555a8182c42c7737a384cfe03a3c7329f646a3bf389c4bcd75379fc85e6c144
    untagged: redis:latest
    untagged: [email protected]:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59
    deleted: sha256:aa4d65e670d6518e5da96ca9d1a76370a942970a8802e6d5cc6bcf058ab12ca7
    deleted: sha256:3bd00d38f5ca70200050477c527cc60cfdf82911d6fe03932e2bcae31a95cfa2
    deleted: sha256:22722fde392d188cfbe5bbd0c2451cc71cf5b000afc0e5114c1066bb5e113ec9
    deleted: sha256:38212b55ef525e86cd726cd83c1a82a6009c68d24771d6e93d439fdc88e66f0e
    deleted: sha256:188c498579cef37b65a93d6448c6b129fa07d5740fc213a18843ff22d80cd10d
    deleted: sha256:2117165cd53c98f13ec7af36c9d8acd239fc541c847efaccb49885decf615d68
    
    Total reclaimed space: 445.2MB
    [email protected]:~$

    You can also use --force or -f flag to proceed to clean without confirmation or use --filter flag to provide filter values (e.g. ‘until=<timestamp>’) to the prune command.

    Example:

    $ docker image prune -a --force --filter "until=2021-01-04T00:00:00"

    Or

    $ docker image prune --filter="label=deprecated"

    Using rmi Command

    You can also use rmi command with docker to remove images.

    It removes (and un-tags) one or more images from the Docker node. If an image has multiple tags, using this command with the tag as a parameter only removes the tag. If the tag is the only one for the image, both the image and the tag are removed.

    This command does not remove images from a registry. Also, you cannot remove an image of a running container unless you use the -f option as with docker image rm command.

    Example:

    [email protected]:~$ docker rmi alpine
    Untagged: alpine:latest
    Untagged: [email protected]:adab3844f497ab9171f070d4cae4114b5aec565ac772e2f2579405b78be67c96
    Deleted: sha256:d4ff818577bc193b309b355b02ebc9220427090057b54a59e73b79bdfe139b83
    Deleted: sha256:72e830a4dff5f0d5225cdc0a320e85ab1ce06ea5673acfe8d83a7645cbd0e9cf
    [email protected]:~$

    You can also use --no-prune to specify not to delete untagged parents.

    Stop Container and Remove Images

    On many occasions, you may need to stop all containers and remove all associated images. You can do that simply with:

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

    The above command will stop and remove all running containers forcefully. Then we can proceed to remove the linked images by using:

    $ docker rmi -f $(docker images -a -q)

    Summary

    Docker is a versatile tool that is an essential part of today’s DevOps engineer’s arsenal and is part of other buzzing technologies like Kubernetes. Docker image management is one of the essential parts of managing and troubleshooting a Docker deployment.

    You should now have a basic idea of the capabilities that docker CLI command offers with its commands like image, images and rmi. Their flags offer further customization and offer advanced filtering and customization options that can help in automation and advanced usage.

    Use docker [COMMAND] help for further details about available options and related help topics.