IntermediateLesson 8 of 26
Volumes and bind mounts
Containers are disposable. Keep important data outside them.
A container's writable layer disappears when the container is removed. Use:
- Named volumes (managed by Docker): best for databases and app data.
- Bind mounts (a host folder): best for live-editing code during development.
- tmpfs: memory-only, for scratch data and secrets.
bash
docker volume create pgdata
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16
docker run -v "$PWD":/app -w /app node:22 npm test # bind mount
docker run --mount type=bind,src="$PWD",dst=/app,readonly myapp