Docker run --mount使所有文件在运行期间位于不同的文件夹中 [英] Docker run --mount make all files available in a different folder during RUN

查看:817
本文介绍了Docker run --mount使所有文件在运行期间位于不同的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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.

然后我有两个问题:


  1. 我可以<$ / path / on / host 中的c $ c> ls 个文件,但不使用它们(例如 cat 他们)。我尝试将 type 更改为例如缓存并使用 source ,如https://devops.stackexchange.com/questions/6078/in- dockerfile是一种避免将文件复制使其可访问的方法,但我无法使其正常工作。

  1. I can ls files inside /path/on/host, but not use them (e.g. cat them). I have tried changing type to e.g. cache and using source 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:


此装载类型允许将上下文或映像中的目录(只读)绑定到构建容器。 / p>

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使所有文件在运行期间位于不同的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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