--mount=type=cache 在 buildkit [英] --mount=type=cache in buildkit

查看:69
本文介绍了--mount=type=cache 在 buildkit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 maven 离线构建在 docker 容器中工作.我已经启用了 buildkit.我已经运行 mvn dependency:go-offline -s ~/checkouts/settings.xml 来缓存主机的 /root/.m2 中的依赖项.我希望在构建 maven 项目的容器中使用它.

I'm trying to get maven offline builds working from within a docker container. I've enabled buildkit. I've run mvn dependency:go-offline -s ~/checkouts/settings.xml to cache the dependencies in /root/.m2 of host machine. I wish to use this inside the container that builds maven project.

这是我的 Dockerfile:

Here is my Dockerfile:

#syntax=docker/dockerfile:experimental
FROM maven:3.6.1-jdk-11 AS build
WORKDIR /
COPY . /
RUN --mount=type=cache,target=/root/.m2 mvn -o install 

FROM scratch
COPY --from=build /admin/admin- 
rest/target/admin-rest.war /webapps/ROOT.war

当我尝试 docker build 这个 Dockerfile 时,我收到以下错误:

When I try to docker build this Dockerfile, I get the following error:

插件 org.codehaus.mojo:build-helper-maven-plugin:3.0.0 或其之一无法解决依赖关系:无法访问中央(https://repo.maven.apache.org/maven2) 在离线模式和神器 org.codehaus.mojo:build-helper-maven-plugin:jar:3.0.0 还没有之前从那里下载过.-> [帮助 1]

Plugin org.codehaus.mojo:build-helper-maven-plugin:3.0.0 or one of its dependencies could not be resolved: Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact org.codehaus.mojo:build-helper-maven-plugin:jar:3.0.0 has not been downloaded from it before. -> [Help 1]

我的 Docker 版本:

My Docker version:

Client:
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77
 Built:             Sat May  4 02:35:57 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.1
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.5
  Git commit:       74b1e89
  Built:            Thu Jul 25 21:19:41 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.5
  GitCommit:        bb71b10fd8f58240ca47fbb579b9d1028eea7c84
 runc:
  Version:          1.0.0-rc6+dev
  GitCommit:        2b18fe1d885ee5083ef9f0838fee39b62d653e30
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

我的期望是否正确,我的主机中的 /root/.m2 需要安装到 docker 容器环境内的 /root/.m2 上?而当我运行 mvn -o install 它需要引用挂载的 /root/.m2?

Am I right in my expectation that /root/.m2 from my host machine needs to get mounted on to /root/.m2 inside the docker container environment? And when I run mvn -o install it needs to refer to the mounted /root/.m2?

我添加了 tail -f/dev/null,删除了第二个构建阶段并将 mvn install 更改为 mvn version 与缓存-mount 指令完整,以便调试.我看到容器内没有任何东西安装到/root/.m2 上.它是空的.(不确定这是否是调试它的正确方法,因为它可能会在构建映像后被卸载)

I added tail -f /dev/null, removed second build stage and changed the mvn install to mvn version with the cache-mount instruction intact in order to debug this. I see that nothing gets mounted on to /root/.m2 inside the container. It is empty. (Unsure if this is the right way to debug this as it may get unmounted after the image is built)

推荐答案

最好把 --mount=type=cache 想象成 docker 中的一个命名卷,由 BuildKit 管理,并且如果 BuildKit 缓存已满或请求修剪,则可能会被删除.下次运行构建时,可能会使用相同的命名卷,从而显着减少下载依赖项所花费的构建时间.虽然非常有用,但这似乎不是您在此处寻找的内容.要使用这样的缓存,您需要在 Dockerfile 中包含 go-offline 作为较早的步骤:

It's best to think of --mount=type=cache as being like a named volume in docker, managed by BuildKit, and potentially deleted if the BuildKit cache gets full or a prune is requested. The next time you run a build, that same named volume may be available, significiantly reducing the build time spent downloading dependencies. While very useful, that doesn't appear to be what you're looking for here. To use a cache like this, you'd need to include the go-offline as an earlier step in the Dockerfile:

#syntax=docker/dockerfile:experimental
FROM maven:3.6.1-jdk-11 AS build
WORKDIR /
# copy just the pom.xml for cache efficiency
COPY ./pom.xml /
# go-offline using the pom.xml
RUN --mount=type=cache,target=/root/.m2 mvn dependency:go-offline
# now copy the rest of the code and run an offline build
COPY . /
RUN --mount=type=cache,target=/root/.m2 mvn -o install 

FROM scratch
COPY --from=build /admin/admin-rest/target/admin-rest.war /webapps/ROOT.war


要将目录从主机挂载到容器中,您似乎要寻找的是绑定挂载.并且使用 BuildKit 的实验设置,这是可用的,但仅适用于构建上下文,而不适用于构建主机上的任何任意目录.为此,您可以将 .m2 目录放在构建上下文目录中,然后在 Dockerfile 中使用以下行:


To mount a directory into the container from the host, what you appear to be looking for is a bind mount. And with BuildKit's experimental settings, that is available, but only to the build context, not to any arbitrary directory on the build host. For that, you can place your .m2 directory in the build context directory and then use the following line in your Dockerfile:

RUN --mount=type=bind,source=./.m2,target=/root/.m2,rw mvn -o install

请注意,如果任何依赖项发生更改,Maven 可能会再次尝试通过网络连接.

Note if any of the dependencies change, then Maven may try to connect over the network again.

这篇关于--mount=type=cache 在 buildkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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