--mount = type =构建套件中的缓存 [英] --mount=type=cache in buildkit

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

问题描述

我正在尝试使docker容器中的Maven脱机构建工作.我启用了buildkit.我已经运行 mvndependency: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版本完整地安装指令以对其进行调试.我看到没有任何内容挂载到容器内的/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 =构建套件中的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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