C.W.K.
Stream
Lesson 03 of 05 · published

Named Volumes — The Right Default for State

~14 min · data, volumes

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Named volumes are Docker-managed storage

You give them a name. Docker stores them under /var/lib/docker/volumes/<name>/_data. They live independently of any container — survive docker rm, survive image rebuilds, only disappear when you explicitly delete them.

Code

Volume CRUD·bash
# Create explicitly (optional — auto-created on first use)
docker volume create pgdata

# List
docker volume ls

# Inspect (where it lives, options, labels)
docker volume inspect pgdata

# Remove (only when no container uses it)
docker volume rm pgdata

# Remove all unused volumes (ASK YOURSELF FIRST)
docker volume prune
Postgres survives container deletion·bash
# First container
docker run -d --name db \
  -v pgdata:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=secret \
  postgres:16

# Insert data
docker exec -it db psql -U postgres -c "CREATE TABLE t(x int); INSERT INTO t VALUES(7);"

# Tear down completely
docker rm -f db

# Recreate
docker run -d --name db \
  -v pgdata:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=secret \
  postgres:16

# Verify
docker exec -it db psql -U postgres -c "SELECT * FROM t;"
# x | 7  ← still there

External links

Exercise

Create three named volumes for a typical web stack: app-pgdata, app-redisdata, app-uploads. Run a Postgres, a Redis, and a stub web container that writes a file to /app/uploads/test.txt. Tear them all down with docker rm -f, recreate with the same volumes, and verify everything is intact.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.