Can we mount subdirectories 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 subdirectories relative to the build file.

For example:

volume: # Just specify the path and let the engine create the volume - / var / lib / mysql

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

# The path on the host relative to the Compose -./cache:/tmp/cache file

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

# The named volume is datavolume: / var / lib / mysql

Is it possible to install a subdirectory of a named volume in 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 suppose I can handle this by setting the data volume in a place like /data , and then in the Dockerfiles for each container, creating symbolic links from subdirectories in the location.

e.g. in the docker-compose.yml file

 volumes: - datavolume:/data 

and then in the dockerfile container

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

I started to take this path, but it became dirty and did not work. Before I either abandon this approach or take the time to debug it, I wanted to make sure that there is no way to simply specify subdirectories of named vollume.

+8
docker docker-compose dockerfile
source share
1 answer

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

datavolume would, datavolume/sql_data would not.

+13
source share

All Articles