What is happening when using ../ with docker-compose volume

时光怂恿深爱的人放手 提交于 2021-02-06 10:40:30

问题


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.


回答1:


The docker build command can only access the directory it is in and lower, not higher, unless you specify the higher directory as the context.

To run the docker build from the parent directory:

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

Or with your example:

 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 

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

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.

See also: Docker: adding a file from a parent directory




回答2:


The statement volumes: ['../:/example'] makes the parent directory of the directory containing docker-compose.yml on the host (../) visible inside the container at /example. Host directory bind-mounts like this, plus some equivalent constructs using a named volume attached to a specific host directory, are the only way a container can write out to the host filesystem.



来源:https://stackoverflow.com/questions/51049460/what-is-happening-when-using-with-docker-compose-volume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!