Managing Volumes

Create, list, inspect and remove volumes with the docker volume command family.

Syntaxdocker volume create|ls|inspect|rm|prune

Docker provides a docker volume command group to manage stored data.

The key commands

  • docker volume create NAME — create a named volume.
  • docker volume ls — list all volumes.
  • docker volume inspect NAME — show details, including where it lives on the host.
  • docker volume rm NAME — delete a volume.
  • docker volume prune — remove all unused volumes.

Backing up a volume

A common trick is to run a throwaway container that mounts the volume and archives it with tar.

Example

Example · bash
# List and inspect volumes
docker volume ls
docker volume inspect pgdata

# Back up a volume to a tar file in the current folder
docker run --rm \
  -v pgdata:/data \
  -v $(pwd):/backup \
  alpine tar czf /backup/pgdata-backup.tar.gz -C /data .

# Remove unused volumes
docker volume prune

When to use it

  • An ops engineer lists all volumes to identify orphaned volumes consuming disk space on a production server.
  • A developer inspects a volume to find its mount point on the host filesystem to examine files directly for debugging.
  • A CI pipeline removes all unused volumes after each test run to prevent disk from filling up on the build agent.

More examples

Create, list, and inspect volumes

Creates a named volume, lists all volumes, and shows detailed metadata including the host mount point.

Example · bash
docker volume create mydata
docker volume ls
docker volume inspect mydata

Remove a specific volume

Removes a named volume and shows how to prune all unused volumes to reclaim disk space.

Example · bash
# Remove a single volume (must not be in use)
docker volume rm mydata

# Remove all stopped containers first, then prune
docker container prune -f
docker volume prune -f

Find volumes used by a container

Extracts the volume name and its mount path inside the container from the inspect output.

Example · bash
docker inspect myapp \
  --format '{{range .Mounts}}{{.Name}} -> {{.Destination}}\n{{end}}'

Discussion

  • Be the first to comment on this lesson.