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 myappDevelopment 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 devConfiguration 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 \
myappBind 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 myappVolumes 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 \
myappSSL Certificates
docker run -d \
-v /etc/letsencrypt:/etc/letsencrypt:ro \
-p 443:443 \
nginxDevelopment Environment
docker run -it --rm \
-v "$(pwd)":/app \
-v /app/node_modules \ # Exclude node_modules
-p 3000:3000 \
node:20 npm run devTroubleshooting
# 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 myappbeginnerStorageUpdated 2025-01-15
- docker
- bind mount
- storage
- development
- host