SyntaxStudy
Sign Up
Docker Named Volumes vs Bind Mounts
Docker Beginner 1 min read

Named Volumes vs Bind Mounts

Docker provides three mechanisms for persisting data outside the container's writable layer: named volumes, bind mounts, and tmpfs mounts. Named volumes are managed entirely by Docker — Docker creates a directory under /var/lib/docker/volumes/ and handles the lifecycle. Bind mounts map a host directory or file directly into the container. tmpfs mounts are in-memory and disappear when the container stops. Named volumes are the recommended way to persist application data (databases, uploaded files). They are portable (can be backed up, migrated, and shared between containers), they work correctly on all platforms including Docker Desktop on macOS and Windows where bind mounts can have performance penalties, and they can be pre-populated by the container image. Bind mounts are the right choice for development: mounting the local source directory into the container means code changes are reflected immediately without rebuilding. They are also used for injecting configuration files into containers. The :ro suffix mounts the bind as read-only inside the container.
Example
# Create a named volume explicitly
docker volume create pgdata

# Use it in a container
docker run -d \
  --name postgres \
  -e POSTGRES_PASSWORD=secret \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16-alpine

# List and inspect volumes
docker volume ls
docker volume inspect pgdata

# Bind mount for development
docker run -d \
  --name app-dev \
  -p 3000:3000 \
  -v "$(pwd)/src:/app/src" \
  -v "$(pwd)/config:/app/config:ro" \
  -e NODE_ENV=development \
  myapp:dev

# tmpfs mount (ephemeral, in-memory)
docker run -d \
  --name api \
  --tmpfs /tmp:size=64m,mode=1777 \
  myapp:latest

# Backup a named volume
docker run --rm \
  -v pgdata:/source:ro \
  -v "$(pwd):/backup" \
  alpine tar czf /backup/pgdata-$(date +%Y%m%d).tar.gz -C /source .