Docker (Dockerfile): Share a directory from host to container

有些话、适合烂在心里 提交于 2019-12-08 10:46:51

问题


I would like to know if there's any way that I can share a directory from my host machine to the docker container (Shared Volume) using Dockerfile.

I understand that we can do that using volumes (-v option) while using docker run. But I couldn't find any way by which I can do that while using an Instruction of Dockerfile.

I already tried VOLUME instruction in Dockerfile but couldn't succeed. Here's some details about my environment:

[me@myHost new]$ tree -L 1
.
|-- docker-compose.yml
|-- Dockerfile
|-- Shared  // This is the directory I wish to share with my containers.

` I was using docker-compose.yml file to mount this directory till now:

volumes:
- ./Shared:/shared  # "Relative Path at the host":"Absolute Path at the container"

But now, due to some reasons, I need to mount it in Dockerfile. I already tried the following but couldn't succeed (It is creating a new empty volume at /shared.):

VOLUME ./Shared:/shared

I could use docker run and save the image by making manual changes in the container, but I wished if I could do that in Dockerfile itself.

Thanks.


回答1:


You can't mount a local directory using commands in a Dockerfile. You must do this with docker run, or a proxy to docker run like docker-compose.



来源:https://stackoverflow.com/questions/42524594/docker-dockerfile-share-a-directory-from-host-to-container

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