1. Overview

In recent years container technologies such as Docker have gained popularity.

Containerization and Virtualization differ in storage management. Containerization uses a layered file system for its storage management. Unlike virtual machines, there isn’t a single command which gives information about its disk usage. Also, there are various Docker objects that constitute the container’s disk usage, mainly images and volumes.

In this short tutorial, we’ll discuss how to calculate the disk space utilized by a container.

2. Setting up an Example

Let’s create a few Docker objects to use as an example.

First, let’s pull an NGINX image:

$ docker image pull nginx:alpine

Now, let’s use this image to create two containers:

$ docker container run --rm -it --name web-server-01 -d nginx:alpine
$ docker container run --rm -it --name web-server-02 -d nginx:alpine

Next, we’ll create a volume and attach it to the container:

$ docker volume create web-server-vol 
$ docker container run --rm -it --name web-server-03 -v web-server-vol:/shared-volume -d nginx:alpine

Now,  let’s check that these objects have been created:

$ docker image list
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        alpine    f246e6f9d0b2   3 weeks ago   23.5MB

$ docker container ls -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
5c672c7d803d   nginx:alpine   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   80/tcp    web-server-03
3e7a463a4374   nginx:alpine   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   80/tcp    web-server-02
dbbece9f7082   nginx:alpine   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   80/tcp    web-server-01

$ docker volume list
DRIVER    VOLUME NAME
local     web-server-vol

As we can see, we have the nginx image, three containers, and one volume. The volume has been attached to web-server-03.

3. Displaying Basic Disk Usage

3.1. Showing Disk Usage After Launch

Docker’s container ls child command shows the disk space utilized by each container:

$ docker container ls --size
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES           SIZE
5c672c7d803d   nginx:alpine   "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   80/tcp    web-server-03   1.09kB (virtual 23.5MB)
3e7a463a4374   nginx:alpine   "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   80/tcp    web-server-02   1.09kB (virtual 23.5MB)
dbbece9f7082   nginx:alpine   "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   80/tcp    web-server-01   1.09kB (virtual 23.5MB)

In the above example, we’ve used the –size option with the command. This option shows the total file size in the last column. This column represents two things: size and virtual size. These are the readable and writable layers of the container.

We must always specify the image when creating a container. The container mounts this image as a read-only layer. Multiple containers can share the same image as they’re immutable. Hence in this example, the size and virtual size are identical for all containers.

3.2. Disk Usage After Changing Files

The writable layer is created on top of the read-only layer. This layer stores any modifications done by the container. Unlike the read-only layer, the writable layer is unique per container. The size column shows the details about it.

To understand this, we’ll modify the writable layer of the web-server-01 container by installing a Vim editor.

Let’s execute an interactive shell on the container:

$ docker exec -it web-server-01 sh

This will create a new shell session in the web-server-01 container and allow us to execute commands.

Next, let’s install a Vim editor using the apk package manager:

# apk add vim

Finally, let’s end the interactive shell session using the exit command:

# exit

Now, let’s check the container size using the same command:

$ docker container ls --size
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS     NAMES           SIZE
5c672c7d803d   nginx:alpine   "/docker-entrypoint.…"   37 minutes ago   Up 36 minutes   80/tcp    web-server-03   1.09kB (virtual 23.5MB)
3e7a463a4374   nginx:alpine   "/docker-entrypoint.…"   37 minutes ago   Up 37 minutes   80/tcp    web-server-02   1.09kB (virtual 23.5MB)
dbbece9f7082   nginx:alpine   "/docker-entrypoint.…"   37 minutes ago   Up 37 minutes   80/tcp    web-server-01   29.1MB (virtual 52.6MB)

Here we can observe that now the size column is showing a writable layer of 29.1 MB.

If we observe carefully, then we can notice that there’s a change in the virtual size as well. This happens because virtual size represents both readable and writable layers of the container. So total disk space utilized by a web-server-01 container is:

23.5 MB(readable layer or image size) + 29.1 MB(writable layer) = 52.6 MB(virtual size)

We should note that this approach doesn’t show the disk space used by the volumes. In the next section, we’ll see how to display the disk usage of the volumes.

4. Displaying Detailed Disk Usage

In the previous section, we saw how easily we can find the basic disk usage of a container. However,  this method doesn’t give the correct result in certain scenarios, like when a container uses one or more volumes. In such cases, we can use the Docker’s system df child command.

Let’s go inside the web-server-03 container and create a file of 2 GB on the volume:

$ docker exec -it web-server-03 sh
/ # cd shared-volume/
/shared-volume # fallocate -l 2G sample-file.img
/shared-volume # exit

In this example, we created a file on a volume – shared-volume which we created when setting up our example.

Now, let’s display its disk usage:

$ docker system df --verbose
Images space usage:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
nginx        alpine    f246e6f9d0b2   3 weeks ago   23.46MB   0B            23.46MB       3

Containers space usage:

CONTAINER ID   IMAGE          COMMAND                  LOCAL VOLUMES   SIZE      CREATED       STATUS       NAMES
5c672c7d803d   nginx:alpine   "/docker-entrypoint.…"   1               1.29kB    2 hours ago   Up 2 hours   web-server-03
3e7a463a4374   nginx:alpine   "/docker-entrypoint.…"   0               1.09kB    2 hours ago   Up 2 hours   web-server-02
dbbece9f7082   nginx:alpine   "/docker-entrypoint.…"   0               29.1MB    2 hours ago   Up 2 hours   web-server-01

Local Volumes space usage:

VOLUME NAME      LINKS     SIZE
web-server-vol   1         2.147GB

From the above output, we can see that the disk usage of the web-server-03 container is approximately 2.17 GB:

23.5 MB(readable layer) + 1.29 kB(writable layer) + 2.147 GB(data on volume) ≈ 2.17 GB

5. Conclusion

In this article, we saw some practical examples to analyze the disk usage of a Docker container.

First, we used the container ls child command to show the basic information.

Then we used the system df child command to display the detailed information.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.