Docker volumes explained: Named volumes, bind mounts and how to manage them

Learn how Docker volumes work with practical examples. Covers named volumes, bind mounts, anonymous volumes, and key commands including create, inspect, ls and prune.

5 lessons · 16 min · Beginner

3 minutes reading time

Written by

Civo Team
Civo Team

Marketing Team at Civo

In Docker, data within a container is not persistent. When a container is restarted, stopped, or removed, the data inside it is lost — the next container starts from a clean state. For databases, stateful applications, and development workflows, this is a problem. Docker volumes solve it by storing data outside the container lifecycle, on the host filesystem or in a Docker-managed location.

This guide covers the three volume types, how to create and manage them, and when to use each.

civo-docker-volume-types

Named volumes

Named volumes are managed entirely by Docker and stored under /var/lib/docker/volumes. They are the recommended approach for persistent application data because they are portable, easy to back up, and work across container restarts and removals.

Create a named volume and mount it to a container:

docker run -it -v demo:/usr/share busybox

Or using the recommended --mount syntax, which is more explicit and easier to read:

docker run -it --mount type=volume,source=demo,target=/usr/share busybox

List all volumes on your system:

docker volume ls

Expected output:

DRIVER VOLUME NAME
local demo

The volume persists after the container exits. Create a new container and mount the same volume — the data will still be there.

Bind mounts

A bind mount links a specific path on your host machine directly into the container. You control exactly where the data lives. This is the standard pattern for development workflows where you want live code or configuration changes reflected in the container immediately.

docker run -it -v /home:/temp busybox

Or with --mount:

docker run -it --mount type=bind,source=/home,target=/temp busybox

Navigate to the mounted directory inside the container and create a file:

cd /temp
touch test
exit

The file is now visible at /home/test on your host machine. Changes made on the host are reflected inside the container and vice versa.

Anonymous volumes

If you do not specify a name or host path, Docker creates an anonymous volume with a randomly generated hash as its name. The data is still persisted but you cannot easily reference the volume again by name.

docker run -it -v /home busybox

Anonymous volumes are useful for temporary scratch space where you do not need to access the data after the container exits.

Managing volumes

Inspect a volume

See details including the mount point on the host, creation date, and driver:

docker volume inspect demo

Expected output:

[
{
"CreatedAt": "2024-01-15T10:23:45Z",
"Driver": "local",
"Mountpoint": "/var/lib/docker/volumes/demo/_data",
"Name": "demo",
"Scope": "local"
}
]

Remove a volume

docker volume rm demo

This permanently deletes the volume and all data inside it. The volume must not be in use by any container.

Remove all unused volumes

docker volume prune

This removes all volumes not currently attached to a container. Add -f to skip the confirmation prompt. Run docker volume ls first to confirm what will be removed — this action is irreversible.

When to use which

Volume typeUse when

Named volume

You need persistent data for a database or stateful app and want Docker to manage the storage

Bind mount

You are developing locally and want changes on your host reflected immediately in the container

Anonymous volume

You need temporary scratch space and do not need to reference the volume again

Civo Team
Civo Team

Marketing Team at Civo

Civo is the Sovereign Cloud and AI platform designed to help developers and enterprises build without limits. We bridge the gap between the openness of the public cloud and the rigorous security of private environments, delivering full cloud parity across every deployment. As a team, we are dedicated to providing scalable compute, lightning-fast Kubernetes, and managed services that are ready in minutes. Through CivoStack Enterprise and our FlexCore appliance, we empower organizations to maintain total data sovereignty on their own hardware.

Our mission is to make the cloud faster, simpler, and fairer. By providing enterprise-grade NVIDIA GPUs and streamlined model management, we ensure that high-performance AI and machine learning are accessible to everyone. Built for transparency and performance, the Civo Team is here to give you total control over your infrastructure, your data, and your spend.

View author profile
Course complete

Nice work, you finished Before Kubernetes: Foundations you need first.

Your next step is up to you - keep building on what you've learned, or put it into practice on Civo.

Next Course

Kubernetes Introduction: what it is and how to get started

3 lessons · 10 min

New to Kubernetes? Start here. Covers what Kubernetes is, the CNCF landscape, k3s, and how container orchestration works. No prior experience required.

Put it into practice

Spin up your first cluster on Civo

Get $250 free credit and launch a production-ready Kubernetes cluster in under 90 seconds.