Skip to main content

Doing Things in Docker

How to Check Disk Space Usage for Docker Images, Containers and Volumes

A collection of tips to let you know how to check disk space usage of Docker Images, Containers and Volumes on your Linux server host.

Wondering how much space does Docker consume on your Linux system?

Primarily, all Docker images, containers and other related entities are located at /var/lib/docker. You can check the size of this directory and get the total disk space used by Docker:

avimanyu@iborg-desktop:~$ sudo du -sh /var/lib/docker
4.9G	/var/lib/docker

But that's not very descriptive and you may have to go further in this directory to see which component uses what space.

Thankfully, Docker has provided tools to get this information in more useful way.

Checking Docker disk space usage [The Docker Way]

The most basic, "Docker" way to know how much space is being used up by images, containers, local volumes or build cache is:

docker system df

When you run this command (use sudo if necessary), you get all disk usage information grouped by Docker components.

avimanyu@iborg-desktop:~$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          4         4         1.065GB   0B (0%)
Containers      4         4         5.705kB   0B (0%)
Local Volumes   7         7         1.108GB   0B (0%)
Build Cache     0         0         0B        0B

This is definitely better than looking at the total size of /var/lib/docker. You can see how much space is consumed by images, containers and volumes.

However, this still does provide a clear picture on which image or volume takes more space.

Actually, it does. The docker system df command has the verbose option -v that gives all these details.

docker system df -v

Here's the verbose output:

avimanyu@iborg-desktop:~$ docker system df -v
Images space usage:

REPOSITORY                               TAG       IMAGE ID       CREATED         SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
ghost                                    4.32.0    b40265427368   8 weeks ago     468.8MB   0B            468.8MB       1
jrcs/letsencrypt-nginx-proxy-companion   latest    037cc4751b5a   13 months ago   24.35MB   0B            24.35MB       1
jwilder/nginx-proxy                      latest    509ff2fb81dd   15 months ago   165MB     0B            165MB         1
mariadb                                  10.5.3    f5d2bcaf057b   20 months ago   407MB     0B            407MB         1

Containers space usage:

CONTAINER ID   IMAGE                                    COMMAND                  LOCAL VOLUMES   SIZE      CREATED        STATUS        NAMES
899cc90e85d9   ghost:4.32.0                             "docker-entrypoint.s…"   1               0B        8 weeks ago    Up 8 weeks    ghost_ghost_6
17b58fdafbce   jrcs/letsencrypt-nginx-proxy-companion   "/bin/bash /app/entr…"   4               571B      3 months ago   Up 2 months   letsencrypt-proxy-companion
58f99f46ee03   jwilder/nginx-proxy                      "/app/docker-entrypo…"   5               5.13kB    3 months ago   Up 2 months   jwilder-nginx-proxy
fb907286b60e   mariadb:10.5.3                           "docker-entrypoint.s…"   1               2B        3 months ago   Up 2 months   ghost_db_1

Local Volumes space usage:

VOLUME NAME                      LINKS     SIZE
ghostdb                          1         434.7MB
jwilder-nginx-with-ssl_acme      2         36.09kB
jwilder-nginx-with-ssl_certs     2         25.12kB
jwilder-nginx-with-ssl_dhparam   1         1.525kB
jwilder-nginx-with-ssl_html      2         1.106kB
jwilder-nginx-with-ssl_vhost     2         556B
ghost                            1         674MB

Build cache usage: 0B

CACHE ID   CACHE TYPE   SIZE      CREATED   LAST USED   USAGE     SHARED

That's nice, right? There are other ways too.

Checking docker image sizes

If you just want to see the Docker images and their sizes, you may also use this command:

docker image ls

It lists all the Docker images on your system with a few details that include the size:

avimanyu@iborg-desktop:~$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
busybox       latest    beae173ccac6   6 weeks ago     1.24MB
ubuntu        latest    fb52e22af1b0   5 months ago    72.8MB
alpine        latest    49f356fa4513   10 months ago   5.61MB
hello-world   latest    d1165f221234   11 months ago   13.3kB

Checking running container sizes

Similarly, if you want to know the size of running Docker containers, you can use the docker ps command:

docker ps --size

You should see a SIZE column added to the output of the command:

avimanyu@iborg-desktop:~$ docker ps --size
CONTAINER ID   IMAGE     COMMAND      CREATED         STATUS         PORTS     NAMES           SIZE
1171dcfb7e06   alpine    "sleep 10"   10 months ago   Up 9 seconds             always-policy   0B (virtual 5.61MB)

Do you notice how it says 0B and then shows a virtual 5.61 MB? The virtual size includes the shared underlying image.

Let us go back to the Linux approach into it more specifically, with the Alpine image and container as a hands-on example.

Using standard Linux commands to analyze Docker disk space usage

Whenever you use the docker pull command or run docker-compose up -d to prepare the launch of applications, this is how you look for image space usage, actually stored on an Ubuntu 20.04 server:

sudo du -sh /var/lib/docker/overlay2/<hash-named-directory>/

Here, Overlay2 is the default Docker storage driver on Ubuntu. You can confirm this by running the docker info command and looking for the Storage Driver:

Storage Driver: overlay2

If this is different than yours, then you're using a different storage driver for Docker. Likewise, the directory location would be named as per the same storage driver. Availability of the storage driver depends upon kernel support.

Specific Image Disk Usage

If you are looking for the locations of specific images, you can use the Docker inspect command for the pulled image. Say, for example, I've pulled the alpine image with docker pull alpine. Run the following command to inspect it:

$ docker inspect alpine

Once you run the command, you'll notice three fields inside the Data subsection under GraphDriver:

...
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/64c9c0cf8c9cfb0e2168071df0652a317d49f58a68fe86e4a9a9a525ab9e365e/merged",
                "UpperDir": "/var/lib/docker/overlay2/64c9c0cf8c9cfb0e2168071df0652a317d49f58a68fe86e4a9a9a525ab9e365e/diff",
                "WorkDir": "/var/lib/docker/overlay2/64c9c0cf8c9cfb0e2168071df0652a317d49f58a68fe86e4a9a9a525ab9e365e/work"
            },

...

Based on the above information, you can see that the (mentioned earlier in the du command syntax) in this case is 64c9c0cf8c9cfb0e2168071df0652a317d49f58a68fe86e4a9a9a525ab9e365e.

Here, you can run the following command to reveal the amount of space being used by the Alpine image:

avimanyu@iborg-desktop:~$ sudo du -sh /var/lib/docker/overlay2/64c9c0cf8c9cfb0e2168071df0652a317d49f58a68fe86e4a9a9a525ab9e365e
6.0M	/var/lib/docker/overlay2/64c9c0cf8c9cfb0e2168071df0652a317d49f58a68fe86e4a9a9a525ab9e365e

Similarly, like images, containers are also stored inside the same storage driver based directory.

/var/lib/docker/overlay2

Specific Container Disk Usage

If you are looking for the locations of specific containers, you can again use the inspect command on Docker for the running container. Say, for example, I've run the alpine container with docker run -ti -d alpine . When you run docker ps, you'll see that it's running:

$ docker ps
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS         PORTS     NAMES
cb341d6a28fa   alpine    "/bin/sh"   6 seconds ago   Up 5 seconds             confident_banzai

Here, the container has been randomly named confident_banzai. So let's inspect it:

$ docker inspect confident_banzai

Once you run the above command, you'll notice all the four fields mentioned earlier inside the Data subsection under GraphDriver. These locations are where the container data is physically stored on your host system just as you saw for images:

...
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/d734685e284c92bdcb6063ac292a48813f30f4b0b2dd6fa2882279c569e506a3-init/diff:/var/lib/docker/overlay2/64c9c0cf8c9cfb0e2168071df0652a317d49f58a68fe86e4a9a9a525ab9e365e/diff",
                "MergedDir": "/var/lib/docker/overlay2/d734685e284c92bdcb6063ac292a48813f30f4b0b2dd6fa2882279c569e506a3/merged",
                "UpperDir": "/var/lib/docker/overlay2/d734685e284c92bdcb6063ac292a48813f30f4b0b2dd6fa2882279c569e506a3/diff",
                "WorkDir": "/var/lib/docker/overlay2/d734685e284c92bdcb6063ac292a48813f30f4b0b2dd6fa2882279c569e506a3/work"
            },
            "Name": "overlay2"
        },
...

Now you can use the du command again:

avimanyu@iborg-desktop:~$ sudo du -sh /var/lib/docker/overlay2/d734685e284c92bdcb6063ac292a48813f30f4b0b2dd6fa2882279c569e506a3
32K	/var/lib/docker/overlay2/d734685e284c92bdcb6063ac292a48813f30f4b0b2dd6fa2882279c569e506a3

Unlike Docker images and containers, the physical locations for volumes is pretty straightforward. Volumes are located at:

/var/lib/docker/volumes/

Therefore, you can run the following command to know the entire Docker volume disk space usage on your system:

avimanyu @iborg-desktop:~$ sudo du -sh /var/lib/docker/volumes/
60K	/var/lib/docker/volumes/

Specific Volume Disk Usage

In this case, there are two types primarily. One is regular Docker volumes and the other is bind mounts.

Docker Volumes

If you are looking for the locations of specific volumes, you can use the docker volume ls command first and check the volume name or ID. Say, for example, I've run the alpine container with the following command with a volume:

docker run -ti -d --name alpine-container -v test-data:/var/lib/app/content alpine

Now, a volume named test-data will automatically get created. Let's now create a file named test.md inside this location:

$ docker exec alpine-container sh -c "touch /var/lib/app/content/test.md"

Verify the file has indeed been created:

$ docker exec -ti alpine-container sh
/ # ls /var/lib/app/content/
test.md
/ # exit

When you run docker volume ls, the volume named test-data would be listed:

$ docker volume ls
DRIVER    VOLUME NAME
local     d502589845f7ae7775474bc01d8295d9492a6c26db2ee2c941c27f3cac4449d1
local     e71ee3960cfef0a133d323d146a1382f3e25856480a727c037b5c81b5022cb1b
local     test-data

Finally, you can confirm the actual location of the file on your host system:

$ sudo ls -l /var/lib/docker/volumes/test-data/_data
total 0
-rw-r--r-- 1 root root 0 Oct  6 23:20 test.md

Therefore, the path for the mounted volume is always located inside a directory named _data inside the respective volume directory.

So, you can use the du command here again for specific volumes!:

avimanyu@iborg-desktop:~$ sudo du -sh /var/lib/docker/volumes/test-data/_data
4.0K	/var/lib/docker/volumes/test-data/_data

Always remember to note the volume name every time you want to find out how much space your volume is using.

Bind Mounts

This is the only exception in Docker where you have to use a Linux approach to monitor disk space usage. At the same time, it is always preferable to stop the running containers first.

$ mkdir /home/avimanyu/test-data
$ docker run -ti -d --name alpine-container -v /home/avimanyu/test-data:/var/lib/app/content alpine

In this case, a bind mounted volume named test-data will become available on the container side as /var/lib/app/content.

avimanyu@iborg-desktop:~$ sudo du -sh /home/avimanyu/test-data
4.0K	/home/avimanyu/test-data

You can also test the same thing inside the container:

avimanyu@iborg-desktop:~$ sudo docker exec -ti alpine-container sh
/ # du -sh /var/lib/app/content
4.0K	/var/lib/app/content

As you can see, both the sizes reported above are the same because they are actually bind mounts.

Docker logs on the host are always stored in volumes. Using the described way in this section, users can also navigate and find that out by looking at docker volumes disk space usage. This varies from app to app and the location of log files in the app volumes.

Bonus Tips

Based on what you learned till now, very obviously, you can also use the following command to fetch the disk usage of images and containers together:

sudo du -sh /var/lib/docker/overlay2

Docker logs on the host are always stored in volumes. A usually large Docker volume would most likely indicate that logs have been piling up and are being managed inefficiently.

How to Check Docker Logs [Stored or Real Time]
Explore and learn some interesting ways to make log management easier on Docker.

Using the described way in the volumes' section of this article, users can also navigate and mitigate this by looking at docker volumes disk space usage. This varies from app to app and the location of log files in the app volumes.

Summary

In this tutorial, I've taken a generic Linux based approach to show you how to find out disk space occupancy of Docker images, containers and volumes residing on your Linux server at the host level. You also learned how to do it the preferred (Docker) way.

If you want to share any feedback, comment or suggestion towards this approach, please leave your thoughts in the comment section below

Avimanyu Bandyopadhyay
Website @iAvimanyu Facebook Kolkata, West Bengal, India