不要在Docker版本上下载所有Maven依赖项 [英] Do not download all Maven dependencies on a Docker build

查看:704
本文介绍了不要在Docker版本上下载所有Maven依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道如何修复Dockerfile,然后执行什么命令。

我想要创建一个Docker文件,然后构建一个Maven项目。 >

我想知道如何运行构建,以便在源代码(位于src /目录中)没有时,每次构建时都不会下载所有Maven依赖项



这是我的Dockerfile文件:

  FROM maven: 3.3.9-jdk-8 

运行mkdir -p / usr / src / app

WORKDIR / usr / src / app

RUN cd / usr / src / app

添加pom.xml / usr / src / app

运行mvn依赖关系:resolve

添加src / usr / src / app

运行mvn包

ENTRYPOINT [mvn]
CMD [package]
/ pre>

我应该运行 docker run -rm -it toolbox 命令或 docker build -t toolbox。命令?



以上这两个命令运行正常,除了他们都下载所有MVE n依赖关系,即使源代码没有被触摸。

解决方案

这是Docker的工作原理。每次您执行 docker运行时,它将创建一个新的容器,该容器无法访问旧容器中的文件。所以,它下载它需要的所有依赖。您可以通过声明外部卷来规避这一点。看看Maven的 Dockerfile ,它声明一个卷 /root/.m2 。因此,您可以使用主机中的目录,并通过 -v 选项将其附加到此卷。您的Docker命令将是,/ b

 `docker运行-v< directory-in-your-host>:/ root /。 m2< other-options-and-commands> 

每次运行新的 docker运行在下载依赖关系之前,Maven将会查看您的本地目录。



但是,我的问题是为什么不先创建应用程序,并使用生成的jar创建码头影像,除非您有任何具体原因。您可以使用java基本图像创建自己的Dockerfile,或者直接使用 docker-maven-plugin 之一,如 spotify 可用。这使得你的生活更容易。


I'm trying to create a Dockerfile to then build a Maven project.

I wonder how to fix the Dockerfile and what command to then execute.

I would like to know how to run the build so that it does NOT download all the Maven dependencies every time it builds when the source code, sitting in the src/ directory, has NOT changed.

Here is my Dockerfile file:

FROM maven:3.3.9-jdk-8

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

RUN cd /usr/src/app

ADD pom.xml /usr/src/app

RUN mvn dependency:resolve

ADD src /usr/src/app

RUN mvn package

ENTRYPOINT ["mvn"]
CMD ["package"]

Should I run the docker run --rm -it toolbox command or the docker build -t toolbox . command ?

Both of these above commands run fine, except that they both download all the Maven dependencies even if the source code has not been touched.

解决方案

That's how Docker works. Every time you do docker run, it creates a new container which does not have any access to the files in the old container. So, it download all dependencies it requires. You can circumvent this by declaring an external volume. Looking at the Dockerfile of Maven, it declares a volume /root/.m2. So, you can use a directory in your host machine and attach it to this volume by -v option. Your Docker command would be,

`docker run -v <directory-in-your-host>:/root/.m2 <other-options-and-commands>

Every time you run a new docker run, Maven will look into your local directory before downloading the dependency.

However, my question is why don't you build your app first and use the resulting jar to create the docker images unless you have any specific reasons. You can create your own Dockerfile using java base image or simply use one of the docker-maven-plugin like spotify available out there. That makes your life a lot easier.

这篇关于不要在Docker版本上下载所有Maven依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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