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

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

问题描述

码头组合文件 https://docs.docker .com / compose / compose-file /#/ volumes-volume-driver 显示了相对于撰写文件安装主机子目录的各种方法。

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.

例如:


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

volumes: # Just specify a path and let the Engine create a volume - /var/lib/mysql

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

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

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

# Path on the host, relative to the Compose file - ./cache:/tmp/cache

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

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

#命名卷
- datavolume:/ var / lib / mysql

# 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

我假设我可以通过安装来管理数据量到 / data 之类的位置,然后在Dockerfiles中为每个容器创建符号链接,从子目录到位置。

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.

例如在docker-compose.yml文件中

for example in a docker-compose.yml file

volumes:
  - datavolume:/data

然后在容器Dockerfile中

and then in the container Dockerfile

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

我开始走这条路,但是变得凌乱而且不起作用。在我放弃这种方法或投入调试时间之前,我想确保没有办法仅仅指定一个命名的vollume的子目录。

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.

推荐答案

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

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 会, datavolume / sql_data 不会。

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

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