Code Reference

Volumes

Docker · Reference cheat sheet

Volumes

Docker · Reference cheat sheet


📋 Overview

Volumes persist data beyond a container’s lifecycle and share files between host and container. Prefer named volumes for databases; bind mounts for live code in development.

🔧 Core concepts

TypeBehavior
Named volumeDocker-managed storage
Bind mountHost path mounted into container
Anonymous volumeUnnamed; easy to lose track of
tmpfsIn-memory mount
FlagExample
-v name:/pathNamed volume
-v /host:/containerBind mount
--mountExplicit long form

💡 Examples

Named volume for Postgres:

docker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/data postgres:16

Compose volume:

services:
  db:
    image: postgres:16-alpine
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

Dev bind mount:

docker run --rm -v "${PWD}:/app" -w /app node:22 npm test

⚠️ Pitfalls

  • Bind-mounting over a path that had image data hides the image content.
  • Permissions (UID/GID) on Linux bind mounts often break writes — match users.
  • docker compose down -v deletes named volumes — destructive.

On this page