HxHippy

Bind Mounts

Mount host directories into containers with bind mounts.

Last updated: 2025-01-15

Bind Mounts

Mount a host directory or file directly into a container.

Bind Mount Syntax

# Short syntax
docker run -v /host/path:/container/path myapp

# Long syntax (recommended)
docker run --mount type=bind,source=/host/path,target=/container/path myapp

# With options
docker run --mount type=bind,source=/host/path,target=/container/path,readonly myapp

Development Use Case

# Mount source code for hot reloading
docker run -d \
  -v $(pwd)/src:/app/src \
  -p 3000:3000 \
  node-dev

# Mount with current directory
docker run -d \
  -v "$(pwd)":/app \
  -w /app \
  node:20 npm run dev

Configuration Files

# Mount single file
docker run -d \
  -v /host/nginx.conf:/etc/nginx/nginx.conf:ro \
  nginx

# Mount directory read-only
docker run -d \
  -v /host/config:/app/config:ro \
  myapp

Bind Mount Options

Option Description
ro Read-only
rw Read-write (default)
z Shared SELinux label
Z Private SELinux label
# Read-only mount
docker run -v /host/data:/data:ro myapp

# SELinux systems
docker run -v /host/data:/data:z myapp

Volumes vs Bind Mounts

Feature Volumes Bind Mounts
Location Docker manages You specify
Backup Easy Manual
Performance Optimized Host dependent
Use case Production Development
Portability Portable Host-specific

Common Patterns

Log Collection

docker run -d \
  -v /var/log/myapp:/app/logs \
  myapp

SSL Certificates

docker run -d \
  -v /etc/letsencrypt:/etc/letsencrypt:ro \
  -p 443:443 \
  nginx

Development Environment

docker run -it --rm \
  -v "$(pwd)":/app \
  -v /app/node_modules \  # Exclude node_modules
  -p 3000:3000 \
  node:20 npm run dev

Troubleshooting

# Check mount
docker inspect --format='{{json .Mounts}}' container | jq

# Permission issues (run as host user)
docker run -u "$(id -u):$(id -g)" -v "$(pwd)":/app myapp

# SELinux issues
docker run -v /host/path:/container/path:z myapp
beginner Storage Updated 2025-01-15
  • docker
  • bind mount
  • storage
  • development
  • host