Docker run --mount 在 RUN 期间使所有文件在不同的文件夹中可用 [英] Docker run --mount make all files available in a different folder during RUN
问题描述
我想在 RUN
语句期间使主机上的文件夹可用.也就是类似于用-v
运行容器的效果:
I want to make a folder on my host machine available during a RUN
statement. That is, similar to the effect of a container run with -v
:
docker run -v /path/on/host:/path/in/container mycontainer:tag
在容器中,这给了我 /path/in/container
以及 path/on/host
中的所有文件/文件夹.
In the container this gives me /path/in/container
with all files/folder in path/on/host
.
为此,我正在尝试 https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md:
RUN --mount=type=bind,target=/path/on/host
这在 RUN
期间为我提供了一个文件夹 /path/on/host
.
This gives me a folder /path/on/host
during the RUN
.
然后我有两个问题:
我可以在
/path/on/host
中ls
文件,但不能使用它们(例如cat
它们).我尝试将type
更改为例如cache
并使用source
就像在 https://devops.stackexchange.com/questions/6078/in-a-dockerfile-is-there-a-way-to-avoid-copying-files-to-make-them-accessible-t,但我不能让它工作.
I can
ls
files inside/path/on/host
, but not use them (e.g.cat
them). I have tried changingtype
to e.g.cache
and usingsource
like in https://devops.stackexchange.com/questions/6078/in-a-dockerfile-is-there-a-way-to-avoid-copying-files-to-make-them-accessible-t, but I cannot make it work.
我不知道如何在RUN
图像"中使用不同的路径,即 /path/in/container
而不是 /path/on/host
.
I cannot figure out how to have a different path inside the "RUN
image", that is, /path/in/container
instead of /path/on/host
.
推荐答案
我想你误解了 RUN --mount=type=bind...
语法的用途.来自文档:
I think you have misunderstood what the RUN --mount=type=bind...
syntax is for. From the documentation:
此挂载类型允许将上下文或映像中的目录(只读)绑定到构建容器.
This mount type allows binding directories (read-only) in the context or in an image to the build container.
换句话说,这不允许您在构建阶段访问任意主机目录.它不是类似于 docker run
上的 -v
命令行选项.它只允许您:
In other words, this does not permit you to access arbitrary host directories in the build stage. It is not an analog to the -v
command line option on docker run
. It only permits you to:
- 从构建上下文中挂载目录,或者
- 在多阶段构建中从另一个阶段装载目录
例如,我可以这样做,将目录从一个构建阶段安装到后续构建阶段:
So for example I can do this do mount a directory from one build stage into a subsequent build stage:
# syntax=docker/dockerfile:experimental
FROM centos AS centos
FROM alpine
RUN --mount=type=bind,from=centos,source=/,target=/centos ls /centos > /root/centos.txt
或者,如果我的构建上下文中有一个名为 example
的目录,我可以这样做以在构建过程中挂载它:
Or if I have a directory named example
in my build context, I can do this to mount it during the build process:
# syntax=docker/dockerfile:experimental
FROM centos AS centos
FROM alpine
RUN --mount=type=bind,source=example,target=/data cp /data/* /root/
您正在使用的语法(未指定 from
)...
The syntax you're using (with no from
specified)...
RUN --mount=type=bind,target=/path/on/host
...只需将构建上下文的根目录安装在容器内的 /path/on/host
上.请记住,target
指定了容器内的安装点.例如,如果我的构建上下文如下所示:
...simply mounts the root of your build context on /path/on/host
inside the container. Remember that target
specifies the mountpoint inside the container. E.g., if my build context looks like this:
.
├── Dockerfile
└── example
└── README.md
而example/README.md
包含:
This is a test.
Dockerfile
包含一个 RUN
选项,类似于您正在使用的选项:
And the Dockerfile
contains a RUN
option similar to what you're using:
# syntax=docker/dockerfile:experimental
FROM centos AS centos
FROM alpine
RUN --mount=type=bind,target=/data cat /data/example/README.md > /root/README.md
那么当镜像构建完成后,/root/README.md
就有了example/README.md
的内容.
Then when the image is built, /root/README.md
has the contents of example/README.md
.
这篇关于Docker run --mount 在 RUN 期间使所有文件在不同的文件夹中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!