Script Valley
Docker: Complete Course
Docker FundamentalsLesson 1.5

How Docker volumes work and when to use them

named volumes, bind mounts, tmpfs mounts, data persistence, volume inspect, -v flag, volume vs bind mount

Persisting Data Beyond a Container's Life

Docker storage types diagram

Container filesystems are ephemeral — delete the container and the data is gone. Volumes solve this.

Named Volumes

Docker manages named volumes. They live at a Docker-controlled path on the host and persist across container restarts and removals.

# Create a named volume
docker volume create pgdata

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

# Inspect volume location
docker volume inspect pgdata

Bind Mounts

Bind mounts map a specific host path into the container. This is the go-to for local development — your editor changes files on the host and the container sees them instantly.

docker run -d \
  -p 3000:3000 \
  -v $(pwd)/src:/app/src \
  my-node-app

When to Use Each

Use named volumes for production databases and persistent state that Docker should manage. Use bind mounts for development workflows where you want live code reload. Never use bind mounts for production database storage — the host path can vary between machines.