Understanding Volumes in docker-compose.yml
Volumes in Docker Compose are persistent data storage mechanisms that allow data to survive container restarts, updates, or complete removal. They're one of the most important concepts in containerization because containers are ephemeral by default - when a container stops, any data created inside it is lost forever.
Core Concept: What Volumes Solve
Without volumes, you'd lose:
- Database records when updating your database container
- Uploaded files when restarting your web server
- Configuration changes made during runtime
- User data generated while your application is running
Volume Types in docker-compose.yml
Let's explore the three main types with specific examples:
1. Named Volumes
These are managed by Docker and referenced by name. Perfect for persistent data like databases.
version: '3.8'
services:
postgres:
image: postgres:15
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: example
volumes:
db_data: # This declares a named volume
In this example:
db_data is a volume managed by Docker
- It's mounted to
/var/lib/postgresql/data inside the container
- This ensures PostgreSQL's data survives container restarts
- The volume appears in
docker volume ls and can be backed up
2. Bind Mounts
These map a specific folder on your host machine to a location in the container.
version: '3.8'
services:
node_app:
build: ./app
ports:
- "3000:3000"
volumes:
- ./app/src:/app/src # Maps your local src directory to container
- ./app/logs:/app/logs # Maps logs directory
This is ideal for development because:
- Code changes in your local
./app/src directory immediately appear in the container
- Log files written to
/app/logs in the container appear in your local directory
- No need to rebuild the image when source code changes
3. Anonymous Volumes
These are managed by Docker but without explicit names:
version: '3.8'
services:
web:
image: nginx
volumes:
- /var/cache/nginx # Anonymous volume
This creates a volume for /var/cache/nginx but Docker assigns a random ID. Less common in practice.
Real-World Examples
Example 1: WordPress with MySQL
version: '3.8'
services:
wordpress:
image: wordpress:latest
ports:
- "8080:80"
volumes:
- wp_content:/var/www/html/wp-content # Store themes, plugins, uploads
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_PASSWORD: example
depends_on:
- db
db:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql # Persist database contents
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: wordpress
volumes:
wp_content: # WordPress files (themes, plugins, uploads)
db_data: # MySQL database files
This setup:
- Persists all WordPress uploads, themes, and plugins in
wp_content volume
- Stores all MySQL data in
db_data volume
- Allows you to upgrade containers without losing any data
Example 2: Development Environment
version: '3.8'
services:
node_app:
build: .
ports:
- "3000:3000"
volumes:
- ./:/app # Mount whole project for development
- /app/node_modules # But exclude node_modules (performance)
command: npm run dev
This configuration:
- Mounts your entire local directory to
/app in the container
- Creates an anonymous volume for
/app/node_modules to prevent the container's node_modules from being overwritten by the host
- Enables live code reloading for development
Example 3: Multiple Volume Types Together
version: '3.8'
services:
web:
image: nginx:alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro # Configuration file (read-only)
- ./website:/usr/share/nginx/html # Website content
- nginx_logs:/var/log/nginx # Log persistence
ports:
- "80:80"
volumes:
nginx_logs: # For persistent logs
Here we're using:
- A bind mount for configuration (
:ro makes it read-only)
- A bind mount for website content (for easy editing)
- A named volume for logs (for persistence and potential log rotation)
Advanced Volume Features
Volume Options
version: '3.8'
services:
app:
image: myapp
volumes:
- type: volume
source: production_data
target: /data
volume:
nocopy: true # Don't copy existing container data to the volume
volumes:
production_data:
driver: local
driver_opts:
type: 'nfs'
o: 'addr=10.0.0.1,nolock,soft'
device: ':/path/to/nfs/share'
This example:
- Uses the long-form volume syntax for advanced options
- Configures an NFS volume for shared storage
- Uses the
nocopy option to improve performance for empty volumes
Volume for Backups
version: '3.8'
services:
db:
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data
backup:
image: postgres:15
volumes:
- postgres_data:/source/data:ro # Read-only access to database files
- ./backups:/backups # Backup destination on host
command: pg_basebackup -h db -D /backups/latest -U postgres -v -P
depends_on:
- db
volumes:
postgres_data:
This setup:
- Creates a separate backup service that can access the database volume
- Makes database access read-only (
:ro) for safety
- Stores backups on the host machine
Common Patterns and Best Practices
-
For databases: Always use named volumes, never bind mounts
volumes:
- db_data:/var/lib/mysql
-
For development: Use bind mounts for code, but named volumes for dependencies
volumes:
- ./src:/app/src
- node_modules:/app/node_modules
-
For configuration: Use bind mounts with read-only flag
volumes:
- ./config.json:/app/config.json:ro
-
For performance (especially on macOS/Windows): Consider delegated/cached options
volumes:
- ./src:/app/src:delegated
Volumes are what make Docker practical for real-world applications by solving the persistence problem inherent in containerization. They bridge the gap between the immutability of containers and the need for state in most applications.