FG
☁️ Cloud & DevOpsDocker

Define host volumes in the `volumes` section of the Compose file

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

It seems that there is not the possibility to mount a host directory using the `volumes` section using the versrion 2 I would propose an configuration example [code block] My use case: docker-compose.yml [code block] Override docker-compose.prod.yml [code block]

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Update Docker Compose File to Use Version 3 for Host Volumes

Medium Risk

Docker Compose version 2 does not support the mounting of host directories in the `volumes` section as effectively as version 3. This limitation can lead to issues when trying to define host volumes for services in your Docker Compose configuration.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Compose File Version

    Change the version of your Docker Compose file from version 2 to version 3 to enable support for host volume mounting.

    yaml
    version: '3'
  2. 2

    Define Host Volumes

    In the `volumes` section of your Docker Compose file, specify the host directory you want to mount to the container. Ensure the path is correct and accessible.

    yaml
    volumes:
      my_volume:
        driver: local
        driver_opts:
          type: none
          o: bind
          device: /path/on/host
  3. 3

    Update Service Configuration

    In the service definitions, reference the newly defined volume to ensure it is mounted correctly.

    yaml
    services:
      my_service:
        image: my_image
        volumes:
          - my_volume:/path/in/container
  4. 4

    Test the Configuration

    Run the updated Docker Compose configuration to ensure that the host volume is mounted correctly and that the service starts without errors.

    bash
    docker-compose up -d
  5. 5

    Verify Volume Mount

    Check inside the running container to confirm that the host directory is correctly mounted. You can do this by executing a shell in the container and listing the contents of the mount point.

    bash
    docker exec -it <container_id> ls /path/in/container

Validation

After performing the steps, run 'docker-compose up -d' and check the logs for any errors. Then, verify the host directory is accessible from within the container by executing the provided command to list the contents.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

dockerdocker-composecontainersstale