简体   繁体   English

在Docker中将目录挂载到命名卷

[英]Mounting a directory to a named volume in Docker

I want the source code of my project to be accessible via a named volume (rather than using bind mount method). 我希望可以通过命名卷(而不是使用bind mount方法)访问项目的源代码。 But there is a confusion for me, I don't know how should I mount my host directory to a named volume. 但是我感到困惑,我不知道如何将主机目录挂载到命名卷。 So, I define a volume in my docker-compose.yml: 因此,我在docker-compose.yml中定义了一个卷:

volumes:
   appData:

services:
   nginx:
      .
      .
      .
      volumes:
         - appData:/usr/share/data

Everything works, but then how should I add my host file to the named volume? 一切正常,但是如何将主机文件添加到命名卷中? And when. 什么时候。

Unfortunately, there is no such straightforward way to achieve this as if now. 不幸的是,没有像现在这样简单的方法可以实现这一目标。 Docker docs suggest using bind mounts OR 3rd party volume plugins in such cases. Docker文档建议在这种情况下使用绑定安装或第三方卷插件。 Couldn't see any near future plan to implement the same. 看不到任何近期实施该计划的计划。

However, you can do it in an alternate way manually - 但是,您可以通过其他方式手动进行操作-

  1. Create the volume(while using compose, volume is created with current directory(PROJECTNAME) as prefix) - 创建卷(使用撰写时,以当前目录(PROJECTNAME)作为前缀创建卷)-
    $ docker volume create ${PROJECTNAME}_appData

  2. Create a dummy container & copy the host directory into the named volume & verify it - 创建一个虚拟容器并将主机目录复制到命名卷中并进行验证-

     $ docker container create --name dummy -v ${PROJECTNAME}_appData:/root alpine #Create $ docker cp ${PWD}/source_myfile.txt dummy:/root/myfile.txt #copy $ docker run -v ${PROJECTNAME}_appData:/root alpine ls -l /root #Verify 

Now when you do a docker-compose up -d , it will skip creating a volume since it already exists. 现在,当您执行docker-compose up -d ,由于已存在卷,它将跳过创建卷。

PS - I understand it's not a standard fix but that's all I could found as a quick solution. PS-我知道这不是标准解决方案,但这就是我可以找到的快速解决方案。 For a permanent fix, you can use 3rd party volume plugins. 对于永久性修复,您可以使用第三方音量插件。
https://docs.docker.com/engine/extend/legacy_plugins/ https://docs.docker.com/engine/extend/legacy_plugins/

Get more details on the issue - 获取有关此问题的更多详细信息-
Ref - https://github.com/moby/moby/issues/25245 参考-https: //github.com/moby/moby/issues/25245

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM