8.2.2.1 Anonymous Volume Creation
A focused guide to Anonymous Volume Creation, connecting core concepts with practical Docker and container operations.
Anonymous volume creation happens automatically whenever a container needs a volume for a particular path — either because the image declared that path with VOLUME, or because a -v flag specified only a container path without a corresponding name or host path — and no explicit volume name was provided.
Creation Triggered by an Image's VOLUME Instruction
An image's own VOLUME declaration automatically triggers anonymous volume creation for any container started from it, unless a specific named volume is explicitly provided instead.
VOLUME /var/lib/postgresql/data
docker run -d postgres:16
Without specifying -v explicitly, this container automatically receives a new anonymous volume for the path declared by the image's VOLUME instruction.
Creation Triggered by an Incomplete -v Flag
Specifying only a container-side path with -v, without a name or host path before the colon, also results in an anonymous volume.
docker run -d -v /app/data myapp:1.0
This creates a new anonymous volume specifically for /app/data, distinct from specifying a named volume or a bind mount.
Avoiding Unintentional Anonymous Volume Creation
Being aware of exactly when anonymous volumes get created — particularly through an image's own VOLUME declaration, which might not be obvious without checking — helps avoid unintentionally accumulating them.
docker image inspect postgres:16 --format '{{json .Config.Volumes}}'
Checking an image's declared volumes before running it reveals whether an anonymous volume will be automatically created unless explicitly avoided.
Explicitly Avoiding Anonymous Creation With a Named Volume
Specifying a named volume for the same path prevents the automatic anonymous volume from being created instead.
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16
Why Understanding Anonymous Volume Creation Matters
Knowing the specific circumstances that trigger automatic anonymous volume creation helps avoid unintentionally ending up with hard-to-identify volumes, particularly when running images whose VOLUME declarations might not be immediately obvious without checking.