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

The docker-compose file shows various ways to mount host sub-directories relative to the compose file.

For example:

  • How to access a host port (bind with ssh -R) from a container?
  • How to run cucumber/selenium tests in Docker?
  • Building images on docker
  • Docker error connecting to host mysql
  • Why does “bundle install --path” break when “bundle install” works?
  • Gitlab CI /var/run/docker.sock: write: broken pipe
  • 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

      - 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.

  • Mount a directory on a host EC2 instance from within a running docker container using beanstalk
  • Build docker image of elasticshearch with sample data
  • Kubernetes - Creating a specific namespace for “services”
  • Docker - Backup Containers in /var/lib/docker/vfs/dir
  • Install and initialize kerberos KDC on docker VM
  • Multiple Docker Containers fails to properly run startup program
  • One Solution collect form web for “Can we mount sub-directories of a named volume in docker?”

    No because compose/config/ check if datavolume/sql_data matches a named volume (in compose/config/

    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.