.m2 的 Docker 缓存在本地不起作用 [英] Docker caching of .m2 doesn't work locally

查看:32
本文介绍了.m2 的 Docker 缓存在本地不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下.gitlab-ci.yml:

variables:
     MAVEN_OPTS: "-Dmaven.repo.local=/root/.m2/repository"

cache:
     key: "M2"
     paths:
        - /root/.m2/repository

直接在gitlab中运行时这样可以正常工作,但是在本地运行时就不行了:

This works correctly when running directly in gitlab, but it doesn't work locally when running as:

gitlab-runner exec docker test

如果我像这样在本地运行它,我可以看到像 成功提取缓存 这样的消息,但随后它会重新下载所有 maven 依赖项,这在我的情况下意味着下载数百兆字节.

If I run it locally like this, I can see messages like Successfully extracted cache, but then it re-downloads all the maven dependencies, which in my case meaning downloading hundreds of megabytes.

我看不出本地运行和远程运行之间的区别,我猜它们的工作方式应该一样?

I cannot see the difference between running it locally and remotely, both of them should work the same I guess?

这个缓存存储在哪里?我可以以某种方式验证确实存储了一些东西吗?

Where is this cache stored? Can I somehow verify there is actually something stored?

推荐答案

这个问题,你需要将你的m2缓存文件夹放入一个卷中,否则容器每次都会从头开始(空缓存),提示maven重新下载所有内容.

As in this issue, you need to put your m2 cache folder into a volume, or the container would start from scratch (empty cache) every time, promting maven to download everything all over again.

该问题也会报告,而不是使用卷:

That issue also reports, instead of using volumes:

如果您将 repo 指向 /cache 中的某些内容,则不需要指定 cache.paths 值;我当前实际使用的版本如下,我没有看到 .m2Removing.."行:

The cache.paths value doesn't need to be specified if you point the repo to something in /cache; the version I'm actually using currently is below, and I don't see the 'Removing..' line for .m2:

image:
  maven:3.3.9-jdk-8

variables:
  MAVEN_OPTS: -Dmaven.repo.local=/cache/maven.repository

test:
  script: "mvn test" 

但使用卷仍然是最便携的解决方案:

But using a volume remains the most portable solution:

我们编辑我们的配置以包含一个名为 host-cache 的本地 docker 主机卷,我们只需将上面的 /cache 引用替换为 /host-cache 不依赖于任何未记录的运行器默认值.

We edit our config to include a local docker host volume named host-cache, and we just replace /cache references above with /host-cache which doesn't depend on any undocumented runner defaults.

这篇关于.m2 的 Docker 缓存在本地不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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