Docker(Dockerfile):从主机到容器共享目录 [英] Docker (Dockerfile): Share a directory from host to container
问题描述
我想知道是否可以使用Dockerfile从主机共享目录到Docker容器(共享卷)。
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.
我了解我们可以在使用docker run时使用卷(-v选项)来做到这一点。但是我在使用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.
我已经在Dockerfile中尝试了VOLUME指令,但未能成功。
以下是有关我的环境的一些详细信息:
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.
`
我正在使用 docker-compose.yml 文件直到现在挂载此目录:
` 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"
但是现在,由于某些原因,我需要将其安装在 Dockerfile 中。我已经尝试了以下操作,但未能成功(它正在/ shared创建一个新的空卷。):
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
我可以使用docker run并通过在容器中进行手动更改来保存图像,但我希望能在Dockerfile本身中做到这一点。
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.
谢谢。
推荐答案
您无法使用Dockerfile中的命令挂载本地目录。您必须使用 docker run
或 docker run
的代理,例如 docker-compose
。
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
.
这篇关于Docker(Dockerfile):从主机到容器共享目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!