Home/Engineer/Docker / Containers/Docker Volumes vs Bind Mount…
Docker / ContainersIntermediateUbuntuDebianCentOSdockervolumebind-mount

Docker Volumes vs Bind Mounts: Choosing Storage, Backing Up Data, and Fixing Permission Errors

A practical rundown of Docker data persistence patterns: volume vs bind-mount differences, backup and restore, and how to fix permission issues.

Contents
  1. Volumes vs Bind Mounts
  2. Volume Management
  3. Bind Mounts
  4. tmpfs Mounts (in-memory, sensitive data)
  5. Volume Backup and Restore
  6. Fixing Permission Issues
  7. Volumes in Docker Compose

Volumes vs Bind Mounts

ItemVolumeBind Mount
LocationManaged by Docker (/var/lib/docker/volumes)Host-specified path
PortabilityHighLow (path-dependent)
Permission managementHandled by DockerHost permissions as-is
Backupdocker volume commandsHost filesystem
DevelopmentNot suitableSuitable (file changes reflected immediately)
ProductionSuitableConditionally 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 prune

Bind 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 bash

tmpfs Mounts (in-memory, sensitive data)

Bash
docker run -d \
  --tmpfs /run:rw,noexec,nosuid,size=64m \
  --tmpfs /tmp \
  nginx

Volume 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 node

Volumes 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: local
#docker#volume#bind-mount
Editorial 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.