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

后端 未结 1 910
独厮守ぢ
独厮守ぢ 2020-12-25 10:39

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.

相关标签:
1条回答
  • 2020-12-25 11:21

    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.

    0 讨论(0)
提交回复
热议问题