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

查看:44
本文介绍了我们可以在 docker 中挂载命名卷的子目录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docker-compose 文件 https://docs.docker.com/compose/compose-file/#/volumes-volume-driver 显示了挂载与撰写文件相关的主机子目录的各种方法.

例如:

<块引用>

volumes: # 只需要指定一个路径,让引擎创建一个卷-/var/lib/mysql

#指定绝对路径映射-/opt/data:/var/lib/mysql

# 主机上的路径,相对于 Compose 文件- ./cache:/tmp/cache

# 用户相对路径- ~/configs:/etc/configs/:ro

# 命名卷- 数据卷:/var/lib/mysql

是否可以在特定位置挂载命名卷的子目录?例如像下面这样的东西,我试过了,但似乎不起作用.

# 命名卷- 数据卷/sql_data:/var/lib/mysql

我假设我可以通过将数据卷挂载到像 /data 这样的位置来管理它,然后在每个容器的 Dockerfiles 中,创建从子目录到位置.

例如在 docker-compose.yml 文件中

卷:- 数据量:/数据

然后在容器中的Dockerfile

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

我开始沿着这条路走下去,但它变得凌乱不堪,无法正常工作.在我放弃这种方法或投入时间调试它之前,我想确保没有办法只指定命名卷的子目录.

解决方案

否,因为 compose/config/config.py#load(config_details) 检查 datavolume/sql_data 是否与命名卷匹配(在 compose/config/validation.py#match_named_volumes())

datavolume 会,datavolume/sql_data 不会.

正如 memetech 指出的那样 在评论中,这是自 2017 年 4 月以来跟踪此问题的一个问题:
moby/moby 问题 32582:"[功能] 允许挂载命名卷的子目录".

在那个问题中,Joohansson 补充说 (见评论)

<块引用>

与此同时,我使用此解决方法将整个卷安装在单独的路径上,然后将其符号链接到子路径.

# 在 Dockerfile 中:运行 mkdir -p/data/subdir运行 ln -s/data/subdir/var/www/subdir

<块引用>

然后照常挂载该卷.
/subdir 必须存在于卷中.

docker run -d -v myvol:/data mycontainer

<块引用>

现在网络服务器读取或写入的任何内容都将存储在卷 subdir 中,并且无法访问其他数据.

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.

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.

for example in a docker-compose.yml file

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

解决方案

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.

这篇关于我们可以在 docker 中挂载命名卷的子目录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆