Docker Compose mounts a volume by defining it in the volumes section of the docker-compose.yml file. This allows persistent data storage outside the container. The volume can be bind-mounted from the host or managed by Docker.
How to Define a Volume in docker-compose.yml
In the docker-compose.yml file, add a volumes key under the service that needs access to the volume. The syntax uses a colon-separated format: SOURCE:DESTINATION, where SOURCE is a path on the host or a named volume, and DESTINATION is the path inside the container. Example:
version: '3.8'
services:
app:
image: nginx
volumes:
- ./data:/usr/share/nginx/html
This binds the local data directory to the container's web root. The ./data directory must exist on the host before running docker-compose up. If it does not, Docker creates an empty directory, which can lead to unexpected behavior if typos are present.
Bind Mount vs Named Volume: When to Use Each
Use a bind mount when you need direct access to specific host files, such as source code for development. Use a named volume when you want Docker to manage the storage location and ensure data persistence across container rebuilds.
| Feature | Bind Mount | Named Volume |
|---|---|---|
| Location Control | Host-specified path | Managed by Docker (usually under /var/lib/docker/volumes/) |
| Use Case | Development, config files, logs | Databases, persistent data |
| Portability | Low (depends on host path) | High (works across environments) |
| Initial Content | Copied from container if path is empty | Always empty unless preloaded |
| Permissions | Follows host file permissions | Set by container process |
Named volumes are created automatically when referenced in docker-compose.yml. Bind mounts require the host path to exist or result in errors if the path is invalid.
How to Mount a Read-Only Volume
To mount a volume as read-only, append :ro to the volume definition. Example:
volumes:
- ./config:/etc/app:ro
This prevents the container from modifying files in /etc/app. Use this for configuration files or static assets that should not change during runtime. Write attempts result in a permission denied error inside the container, which helps catch unintended writes during testing.
Using Environment Variables in Volume Paths
Docker Compose supports environment variables in volume definitions. Use ${VAR_NAME} syntax in docker-compose.yml. Example:
volumes:
- ${DATA_DIR}:/app/data
Set DATA_DIR in a .env file or export it in the shell before running docker-compose up. If the variable is undefined, Docker treats it as an empty string, which may cause errors. Always verify the variable is set using echo $DATA_DIR before running compose.
Mounting Multiple Directories
List each volume on a separate line under the volumes key. Example:
volumes:
- ./src:/app/src
- ./logs:/app/logs
- db-data:/var/lib/postgresql/data
This mounts source code, logs, and a named volume for database storage. Order does not matter, but clarity improves maintenance. Avoid overlapping mount paths, such as mounting ./src to /app and ./config to /app/config, as it can obscure file precedence.
How to Set Permissions for Mounted Files
File ownership inside the container depends on the user running the process. If the container runs as user nginx (UID 101), and the host files are owned by user alice (UID 1000), permission errors may occur. To fix this, ensure the host directory is writable by the container's UID.
On Linux, change ownership with:
sudo chown -R 101:101 ./data
Or run the container with a specific user:
services:
app:
image: nginx
user: "1000:1000"
volumes:
- ./data:/usr/share/nginx/html
This runs the container process as UID 1000, matching the host user. Avoid using root in production containers. For development, use group permissions or Docker Desktop’s file sharing settings on macOS and Windows.
How to Debug Volume Mount Failures
If a volume fails to mount, first check the path in docker-compose.yml. Typos in paths, missing directories, or incorrect permissions cause most issues. Run ls -la on the host path to confirm existence and permissions.
Use docker-compose config to validate the YAML syntax. It outputs the final configuration and highlights invalid volume definitions. Then, run docker-compose up and check the output for errors like invalid bind mount or no such file or directory.
If the container starts but the volume appears empty, verify that the host directory is not empty. Docker does not copy existing container files into a bind mount if the host path exists—even if empty. To copy initial content, temporarily rename the host directory, run the container once, stop it, and merge the data.
For named volumes, inspect them with docker volume inspect VOLUME_NAME. The output shows the mount point on the host. Use this to manually check content or debug access issues.
How Volumes Persist After docker-compose down
Named volumes persist after docker-compose down unless explicitly removed. To delete them, run docker-compose down --volumes. Bind mounts persist because they are regular host directories. Always back up bind-mounted data before system maintenance.
To list all named volumes, run docker volume ls. To remove unused volumes, run docker volume prune. This deletes volumes not referenced by any container, saving disk space. Confirm deletion is intentional, as data loss is permanent.
Using Relative vs Absolute Paths in Bind Mounts
Docker Compose resolves relative paths (e.g., ./data) relative to the location of the docker-compose.yml file. Absolute paths (e.g., /home/user/app/data) are used as-is. Relative paths improve portability across systems. Absolute paths are necessary when mounting system directories like /var/log.
Never use ~ in paths—Docker does not expand it. Use ${HOME} or write the full path. Example:
volumes:
- ${HOME}/backups:/backups
This ensures the path resolves correctly across different user environments.