gpt4 book ai didi

Can we mount sub-directories of a named volume in docker?(我们可以在docker中挂载命名卷的子目录吗?)

转载 作者:bug小助手 更新时间:2023-10-25 16:59:37 36 4
gpt4 key购买 nike



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.

停靠器合成文件https://docs.docker.com/compose/compose-file/#/volumes-volume-driver显示了相对于合成文件挂载主机子目录的各种方法。


For example:

例如:


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.

我假设我可以通过将数据卷挂载到像/data这样的位置,然后在每个容器的Docker文件中创建从子目录到这些位置的符号链接来实现这一点。


for example in a docker-compose.yml file

例如,在docker-compose.yml文件中


volumes:
- datavolume:/data

and then in the container Dockerfile

然后在容器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.

我开始沿着这条路走,但它变得杂乱无章,不起作用。在放弃该方法或花费时间对其进行调试之前,我希望确保不存在仅指定命名卷的子目录的方法。


更多回答

couldn't you mount it somewhere like datavolume:/var/datavolume and in the container do ln -s /var/datavolume/dql_data /var/lib/mysql?

你能不能把它挂载到datavolume:/var/datavolume这样的地方,然后在容器中做ln -s /var/datavolume/dql_data /var/lib/mysql?

I have edited my answer to include a workaround.

我已经编辑了我的答案,包括了一个解决办法。

优秀答案推荐

2023: As noted by Michael Bolli in the comments, that feature is now a work-in-progress:

2023年:正如迈克尔·博利在评论中指出的那样,这一功能现在还在进行中:


PR 45687: "volumes: Implement subpath mount"

PR 45687:“卷:实施子路径装载”



Make it possible to mount subdirectory of a named volume.





2016: 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())

2016:否,因为Compose/CONFIG/CONFIG.PY#LOAD(CONFIG_DETAILS)检查数据卷/SQL_DATA是否与命名卷匹配(在compose/config/validation.py#match_named_volumes())中


datavolume would, datavolume/sql_data would not.

数据卷可以,而数据卷/SQL_Data不可以。


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

正如Memetech在评论中指出的那样,自2017年4月以来一直在跟踪这一问题:白云/白云问题32582:“[Feature]允许挂载命名卷的子目录”。


In that issue, Joohansson adds (see comment)

在这一期中,Joohansson补充道(见评论)



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.




It is not possible.
You can just create normal sub directories into the volume and then in your application link or configure to use that path.

这不可能。您只需在卷中创建普通子目录,然后在应用程序链接中或将其配置为使用该路径。


Let's say you use this compose volume definition :

假设您使用以下合成卷定义:


    volumes:
- myVolume:/mnt/data
volumes:
myVolume:

The volume setup is irrelevant, but only the mounpoint matters, assuing the mount point inside container is "/mnt/data/"

卷设置无关紧要,但重要的是挂载点,容器内的挂载点是“/mnt/data/”


Then in you application just point to sub directories:
logging to /mnt/data/log,
database files to /mnt/data/db
configs to /mnt/config

然后在你的应用程序中只需要指向子目录:logging到/mnt/data/log,数据库文件到/mnt/data/db,文件到/mnt/config


For example doing this in DockeFile

例如,在DockeFile中执行此操作


RUN mkdir -p /mnt/data/log
RUN mkdir /mnt/data/db
RUN mkdir /mnt/data/config
RUN sed ... to change logging path to /mnt/data/log
RUN mv /current/db/data/files /mnt/data/db
RUN ln /mnt/data/db /current/db/data/files
...

更多回答

This feature would be awesome. Also looking for same functionality instead of polluting more named volumes.

这一功能将是非常棒的。也在寻找相同的功能,而不是污染更多的命名卷。

Feature is open as of 2018-07-17 github.com/moby/moby/issues/32582

该功能自2018年07月17日开始开放,网址为:githorb.com/moby/moby/Issues/32582

Feature is now a work-in-progress: github.com/moby/moby/issues/32582#issuecomment-1593512466 -> github.com/moby/moby/pull/45687

功能现在正在开发中:github.com/moby/moby/issues/32582#issuecomment-1593512466->githeb.com/moby/moby/Pull/45687

@MichaelBolli Thank you for your feedback. I will include your comment in the answer as soon as I am able to.

@MichaelBolli感谢您的反馈。我会尽快在回答中包括您的意见。

@MichaelBolli Done. I have edited the answer to include your comment.

@MichaelBolli完成了。我已经编辑了答案,以包括您的评论。

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com