Volumes vs Bind Mounts
| Item | Volume | Bind Mount |
|---|---|---|
| Location | Managed by Docker (/var/lib/docker/volumes) | Host-specified path |
| Portability | High | Low (path-dependent) |
| Permission management | Handled by Docker | Host permissions as-is |
| Backup | docker volume commands | Host filesystem |
| Development | Not suitable | Suitable (file changes reflected immediately) |
| Production | Suitable | Conditionally suitable |
Volume Management
Bash
# Create a volume
docker volume create mydata
# List volumes
docker volume ls
# Inspect volume details
docker volume inspect mydata
# Mount a volume
docker run -d \
-v mydata:/var/lib/postgresql/data \
--name postgres \
postgres:16
# Prune unused volumes
docker volume pruneBind Mounts
Bash
# Basic bind mount
docker run -d \
-v /host/path:/container/path \
nginx
# Read-only mount
docker run -d \
-v /etc/nginx/conf.d:/etc/nginx/conf.d:ro \
nginx
# Mount the current directory (for development)
docker run -it \
-v $(pwd):/app \
-w /app \
node:20 bashtmpfs Mounts (in-memory, sensitive data)
Bash
docker run -d \
--tmpfs /run:rw,noexec,nosuid,size=64m \
--tmpfs /tmp \
nginxVolume Backup and Restore
Bash
# Backup — extract the volume as a tar archive
docker run --rm \
-v mydata:/data \
-v $(pwd):/backup \
alpine tar czf /backup/mydata-$(date +%Y%m%d).tar.gz -C /data .
# Restore
docker run --rm \
-v mydata:/data \
-v $(pwd):/backup \
alpine tar xzf /backup/mydata-20250101.tar.gz -C /data
# Copy between volumes
docker run --rm \
-v source_vol:/from \
-v dest_vol:/to \
alpine cp -a /from/. /to/Fixing Permission Issues
Bash
# Check UID inside the container
docker run --rm nginx id
# Match the host UID
docker run -d \
--user 1000:1000 \
-v $(pwd)/data:/app/data \
myapp
# Set permissions in the Dockerfile
# RUN chown -R node:node /app
# USER nodeVolumes in Docker Compose
YAML
version: '3.8'
services:
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data # named volume
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro # bind mount
app:
image: myapp
volumes:
- ./src:/app/src # bind mount for development
volumes:
pgdata: # volume declaration
driver: localEditorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)Docker documentation ↗pgvector repository ↗
Questions & answers
Ask a question about this guide. We'll review and reply.