Can we mount sub-directories of a named volume in docker?

The docker-compose file https://docs.docker.com/compose/compose-file/#/volumes-volume-driver shows various ways to mount host sub-directories relative to the compose file.

For example:

  • How to delete Unreachable Nodes in Docker Cloud?
  • Localhost doesn't forward requests to oracle docker container
  • Docker 1.12 Swarm Mode - Load balance tasks of the same service on single node
  • Can not write to '/dev/tty' when build Docker images
  • Why can't I use Docker CMD multiple times to run multiple services?
  • Cannot reach files found on host with Docker (Failed to open stream: Connection timed out)
  • volumes: # Just specify a path and let the Engine create a volume
    – /var/lib/mysql

    # Specify an absolute path mapping
    – /opt/data:/var/lib/mysql

    # Path on the host, relative to the Compose file
    – ./cache:/tmp/cache

    # User-relative path
    – ~/configs:/etc/configs/:ro

    # Named volume
    – datavolume:/var/lib/mysql

    Is is possible to mount a sub-directory of a named volume at a specific location? For example something like below, which I tried, but does not seem to work.

    # Named volume
      - datavolume/sql_data:/var/lib/mysql
    

    I am assuming I might be able to manage this by mounting the data volume to a location like /data and then in the Dockerfiles for each container, create symbolic links from the sub-directories to the locations.

    for example in a docker-compose.yml file

    volumes:
      - datavolume:/data
    

    and then in the container Dockerfile

    RUN ln -s /data/sql_data /var/lib/mysql
    

    I started going down this path but it was getting messy and was not working. Before I either abandon that approach or invest the time debugging it, I wanted to make sure there was no way to just specify sub-directories of a named vollume.

  • Write journald metadata to rsyslog
  • List used images from Docker Compose
  • Can't Delete file created via Docker
  • Error while running portia on docker in ubuntu
  • Docker Compose - Command using Container Environment Variable
  • Automatic build with Add from URL
  • One Solution collect form web for “Can we mount sub-directories of a named volume in docker?”

    No because compose/config/config.py#load(config_details) check if datavolume/sql_data matches a named volume (in compose/config/validation.py#match_named_volumes())

    datavolume would, datavolume/sql_data would not.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.