Can we mount sub-directories of a named volume in docker?
Solution 1:
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.
As memetech points out in the comments, the is an issue tracking this since April 2017:moby/moby
issue 32582: "[feature] Allow mounting sub-directories of named volumes".
In that issue, Joohansson adds (see comment)
In the meantime, I use this workaround to mount the whole volume on a separate path and then symlink it to the sub path.
# In the Dockerfile:
RUN mkdir -p /data/subdir
RUN ln -s /data/subdir /var/www/subdir
Then mount the volume as normal.
The/subdir
must exist in the volume.
docker run -d -v myvol:/data mycontainer
Now anything read or written by the webserver will be stored in the volume
subdir
and can't access the other data.