将../与docker-compose卷一起使用时发生了什么 [英] What is happening when using ../ with docker-compose volume

查看:62
本文介绍了将../与docker-compose卷一起使用时发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Docker容器内部将文件写出到我的主机时遇到问题.我认为这是一个特权问题,因此最好不要设置 privileged:True .解决方法是将 ../预先添加到我的 docker-compose.yml 文件中的卷中.例如,

I am having problems with writing files out from inside a docker container to my host computer. I believe this is a privilege issue and prefer not to set privileged: True. A work around for writing out files is by pre-pending ../ to a volume in my docker-compose.yml file. For example,

version: '3'
services:
    example:
        volumes:
         - ../:/example

../在这里到底在做什么?它是否从容器的特权中获取并转到"主机的目录?没有 ../,我无法将文件写出到主机上.

What exactly is ../ doing here? Is it taking from the container's privileges and "going up" a directory to the host machine? Without ../, I am unable to write out files to my host machine.

推荐答案

将路径指定为源而不是卷名,bind将主机路径安装到容器内部的路径.在您的示例中,../在最新版本的docker上的/example的容器内部将可见.

Specifying a path as the source, as opposed to a volume name, bind mounts a host path to a path inside the container. In your example, ../ will be visible inside the container at /example on a recent version of docker.

较早版本的docker只能访问其所在目录和更低目录,而不能访问更高目录,除非您将更高目录指定为上下文.

Older versions of docker can only access the directory it is in and lower, not higher, unless you specify the higher directory as the context.

要从父目录运行docker构建:

To run the docker build from the parent directory:

docker build -f /home/me myapp/Dockerfile 

相对于

docker build -f /home/me/myapp Dockerfile

在作曲家中做同样的事情:

Doing the same in composer:

 #docker-compose.yml
 version: '3.3'    
 services:
   yourservice:
     build:
       context: /home/me
       dockerfile: myapp/Dockerfile

或以您的示例为例:

 version: '3'
 services:
     build: 
        context: /home/me/app
        dockerfile: docker/Dockerfile
     example:
        volumes:
          - /home/me/app:/example

此外,您必须提供完整路径,而不是相对路径.即.

Additionally you have to supply full paths, not relative paths. Ie.

- /home/me/myapp/files/example:/example 

如果您有一个脚本从未知路径生成Dockerfile,则可以使用:

If you have a script that is generating the Dockerfile from an unknown path, you can use:

CWD=`pwd`; echo $CWD

引用当前工作目录.从那里可以添加/..

To refer to the current working directory. From there you can append /..

或者,您可以从一个目录建立映像,或者使用可以与从更高目录运行的映像共享的卷,或者需要将文件输出到stdout并重定向命令的输出从运行它的脚本中找到所需的文件.

Alternately you can build the image from a directory one up, or use a volume which you can share with an image that is run from a higher directory, or you need to output your file to stdout and redirect the output of the command to the file you need from the script that runs it.

另请参阅: Docker:从父目录添加文件

这篇关于将../与docker-compose卷一起使用时发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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