乐闻世界logo
搜索文章和话题

How do I mount a host directory as a volume in docker compose

1个答案

1

In Docker Compose, mounting a host directory as a volume within the container is primarily achieved by using the volumes configuration option under the service definition in the docker-compose.yml file. This ensures the container can access or modify files and directories on the host, commonly used for data persistence or data sharing scenarios.

Example

Assume you have a simple application requiring access to the host data directory /path/to/your/data. You want to mount this directory to /app/data inside the container. Below is the corresponding docker-compose.yml configuration example:

yaml
version: '3.8' services: app: image: your-app-image volumes: - /path/to/your/data:/app/data ports: - "8000:8000"

Explanation

In this configuration:

  • volumes: This is the key field defining volume mounts.
    • /path/to/your/data:/app/data indicates that the host directory /path/to/your/data is mounted to the container directory /app/data. The left side specifies the host path, and the right side specifies the container path, separated by a colon :.

Notes

  • Verify the host directory path /path/to/your/data is correct and that Docker has access permissions to this path.
  • Path formats may vary across operating systems, particularly on Windows, where you may need to adjust the path format or use absolute paths.
  • Ensure the permission settings of the mounted host directory allow the container user to read and write; otherwise, permission issues may occur.

By using this approach, you can easily mount host directories into containers with Docker Compose, enabling data persistence or sharing.

2024年8月10日 00:58 回复

你的答案