dockerdocker-composedocker-stack

Mixing named volumes and bind mounting in Docker?


How does mixing named volumes and bind mounts work? Using the following setup will the paths that are being bind mounted still be available inside the bind mount as they exist in the bind mount? /var/www/html/wp-content/uploads

Using a separate container which I attach to the named volumes, seems to show that it is not the case as those paths are completely empty from the view of the separate container. Is there a way for this to work in a sense?

    volumes:
      - "wordpress:/var/www/html"
      - "./wordpress/uploads:/var/www/html/wp-content/uploads"
      - "./wordpress/plugins:/var/www/html/wp-content/plugins"
      - "./wordpress/themes:/var/www/html/wp-content/themes"

Solution

  • Host volumes: For a host volume, defined with a path in your docker compose file like:

    volumes:
      - "./wordpress/uploads:/var/www/html/wp-content/uploads"
    

    you will not receive any initialization of the host directory from the image contents. This is by design.


    Named volumes: You can define a named volume that maps back to a local directory:

    version: "2"
    
    services:
      your-service:
        volumes:
          - uploads:/var/www/html/wp-content/uploads
    
    volumes:
      uploads:
        driver: local
        driver_opts:
          type: none
          o: bind
          device: /path/on/host/to/wordpress/uploads
    

    This will provide the initialization properties of a named volume. When your host directory is empty, on container creation docker will copy the contents of the image at /var/www/html/wp-content/uploads to /path/on/host/to/wordpress/uploads.


    Nested mounts with Docker: If you have multiple nested volume mounts, docker will still copy from the image directory contents, not from a parent volume.

    Here's an example of that initialization. Starting with the filesystem:

    testvol/
      data-image/
        sub-dir/
          from-image
      data-submount/
      Dockerfile
      docker-compose.yml
    

    The Dockerfile contains:

    FROM busybox
    COPY data-image/ /data
    

    The docker-compose.yml contains:

    version: "2"
    
    services:
      test:
        build: .
        image: test-vol
        command: find /data
        volumes:
          - data:/data
          - subdir:/data/sub-dir
    
    volumes:
      data:
      subdir:
        driver: local
        driver_opts:
          type: none
          o: bind
          device: /path/on/host/test-vol/data-submount
    

    And the named volume has been initialized:

    $ docker run -it --rm -v testvol_data:/data busybox find /data
    /data
    /data/sub-dir
    /data/sub-dir/from-named-vol
    

    Running the test shows the copy comes from-image rather than from-named-vol:

    $ docker-compose -f docker-compose.bind.yml up
    ...
    Attaching to testvol_test_1
    test_1  | /data
    test_1  | /data/sub-dir
    test_1  | /data/sub-dir/from-image
    testvol_test_1 exited with code 0
    

    And docker has copied this to the host filesystem:

    $ ls -l data-submount/
    total 0
    -rw-r--r-- 1 root root 0 Jan 15 08:08 from-image
    

    Nested mounts in Linux: From your question, there appears to be some confusion on how a mount itself works in Linux. Each volume mount runs in the container's mount namespace. This namespace gives the container its own view of a filesystem tree. When you mount a volume into that tree, you do not modify the contents from the parent filesystem, it simply covers up the contents of the parent at that location. All changes happen directly in that newly mounted directory, and if you were to unmount it, the parent directories will then be visible in their original state.

    Therefore, if you mount two nested directories in one container, e.g. /data and /data/a, and then mount /data in a second container, you will not see /data/a from your first container in the second container, only the contents of /data will be there, including any folders that were mounted over top of.