✦ For everyone, free.

Practical knowledge for real and everyday life

Home

4.2.12.1 VOLUME Mount Declaration

A focused guide to VOLUME Mount Declaration, connecting core concepts with practical Docker and container operations.

A VOLUME mount declaration is the act of specifying, within a Dockerfile, exactly which filesystem path should be treated as a mount point for external storage, which Docker honors by automatically creating and mounting a volume at that location whenever a container starts, even without explicit run-time configuration.

Declaring a Single Mount Point

The most basic form declares one path as a volume.

VOLUME /var/lib/mysql

Every container started from this image, regardless of how it is run, has a volume mounted at this exact path, whether anonymous or explicitly specified.

Declaring Multiple Mount Points

VOLUME can declare more than one path, either through repeated instructions or as a single instruction listing multiple paths.

VOLUME ["/data", "/logs"]
VOLUME /data
VOLUME /logs

Both forms result in two separate mount points, each independently backed by its own volume.

What Happens at Container Start

When a container starts, Docker checks each declared volume path; if no volume has been explicitly specified for it, an anonymous volume is created and mounted there automatically, ensuring the declared behavior occurs even without explicit run-time configuration.

docker run -d postgres:16
docker inspect $(docker ps -lq) --format '{{json .Mounts}}'

This reveals the automatically created volume mount, confirming the declaration took effect even without any -v flag being specified.

Declaring Without Necessarily Requiring External Configuration

Because VOLUME triggers automatic volume creation, an image author can ensure persistent storage behavior happens by default, useful for images like databases where losing data due to an overlooked configuration step would be a significant problem.

docker run -d postgres:16
docker rm -f $(docker ps -lq)
docker volume ls

The automatically created volume remains even after the container that used it is removed, since volumes have an independent lifecycle from the containers that mount them.

Why Mount Point Declaration Matters

Declaring mount points explicitly in the image itself provides a safety net, ensuring critical persistent data has somewhere durable to live by default, rather than relying entirely on whoever runs the container remembering to configure storage correctly.