Skip to content

Volumes and Storage

First PublishedByAtif Alam

Containers are ephemeral: when you remove a container, changes inside its filesystem are lost. Volumes and bind mounts give you persistent or host-linked storage.

A named volume is managed by Docker. Data lives in a directory on the host (often under /var/lib/docker/volumes/) and survives container removal. Best for databases and other data that should persist.

Terminal window
# Create a named volume
docker volume create mydata
# Use it in a container
docker run -d -v mydata:/var/lib/mysql mysql:8
# Inspect
docker volume ls
docker volume inspect mydata

In Compose:

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

A bind mount maps a host path into the container. Changes are visible on both sides. Good for config files, source code during development, or when you need direct access to host files.

Terminal window
# Mount host ./app into container /app
docker run -v $(pwd)/app:/app myapp:1.0

In Compose:

volumes:
- ./config:/app/config:ro # :ro = read-only in container

tmpfs is in-memory storage. Nothing is written to disk; it disappears when the container stops. Use for sensitive or temporary data (e.g. secrets, scratch space).

Terminal window
docker run -d --tmpfs /tmp:rw,size=100m myapp:1.0
TypeManaged byPersists after container rm?Use case
Named volumeDockerYes (until volume rm)DB data, persistent app data
Bind mountHost pathYes (host filesystem)Config, source in dev
tmpfsMemoryNoTemp or sensitive data
Terminal window
docker volume create <name>
docker volume ls
docker volume inspect <name>
docker volume rm <name>
docker volume prune # Remove unused volumes
  • Named volumes — Docker-managed, persistent; ideal for databases and app data.
  • Bind mounts — Host path mapped in; good for config and dev source.
  • tmpfs — In-memory; no persistence. Use docker volume commands to manage named volumes.