减小多模块Maven项目的Docker镜像大小

huangapple 未分类评论39阅读模式
英文:

Reducing Docker image size for multi-module maven project

问题

我有一个多模块的Maven项目,包含两个模块:`module1` 和 `module2`。它们的依赖关系几乎相同,大部分在父级 `pom.xml` 中定义。

module1/
    src/...
    pom.xml
module2/
    src/...
    pom.xml
pom.xml

我正在努力创建一个Docker镜像,使我能够将 `module1` 和 `module2` 作为可执行的JAR运行,但不会将父级 `pom.xml` 中的依赖复制两次。

更新
----

为了回答评论中的一些问题,我想创建**一个**Docker镜像,我可以从中运行 `module1` 和 `module2` 的可执行JAR。它们彼此不依赖,完全独立运行。我想创建一个镜像,因为(当然?!)这将有助于节省Docker主机上的磁盘空间。

父级 `pom.xml` 如下:

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<modules>
    <module>module1</module>
    <module>module2</module>
</modules>
<dependencies>
 ...
</dependencies>
英文:

I have a multi-module Maven project consisting of two modules: module1 and module2. Their dependencies, which are almost the same, are mostly defined in the parent pom.xml.

module1/
    src/...
    pom.xml
module2/
    src/...
    pom.xml
pom.xml

I'm struggling to create a Docker image that allows me to run module1 and module2 as executable jars, but does not copy the dependencies in the parent pom.xml twice?

Update

To address some questions in the comments, I would like to create one docker image from which I can run executable jars for module1 and module2. They do not depend on one and other and run completely separately. I want to create one image since (surely?!) it'll help conserve disk space on Docker host.

The parent pom.xml is:

&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;parent&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOST&lt;/version&gt;
&lt;packaging&gt;pom&lt;/packaging&gt;
&lt;name&gt;parent&lt;/parent&gt;
&lt;modules&gt;
    &lt;module&gt;module1&lt;/module&gt;
    &lt;module&gt;module2&lt;/module&gt;
&lt;/modules&gt;
&lt;dependencies&gt;
 ...
&lt;/dependencies&gt;

答案1

得分: 0

以下是翻译好的内容:

我想创建一个Docker镜像,可以从中运行module1和module2的可执行JAR文件。它们彼此不依赖,完全独立运行。我想创建一个镜像,因为(当然?!)这将有助于节省Docker主机上的磁盘空间。

不要这样做。这可能是对Docker的误用。
如果应用程序module1完全不依赖于应用程序module2,您不应该试图将它们耦合在同一个镜像中,只为了获得一个镜像而不是两个镜像。
首先,将使镜像变得更大,作为单独的镜像,但总体上它会创建不希望的耦合:module1镜像上的代码/配置/更改可能会对module2镜像造成副作用,反之亦然。此外,它还将耦合生命周期:您为两个应用程序都创建一个新版本的镜像,而只有一个应用程序可能会发生变化。
小利益带来许多严重的缺点。

英文:

> I would like to create one docker image from which I can run
> executable jars for module1 and module2. They do not depend on one and
> other and run completely separately. I want to create one image since
> (surely?!) it'll help conserve disk space on docker host.

Don't do that. It is probably a misuse of Docker.
If the application module1 doesn't depend at all on the application module2, you should not try to couple them in the same image only to get one image instead of two images.
First, it will first the image bigger as individual one but overall it creates an undesirable coupling : code/configuration/changes on module1 image may cause side effects on module2 image and reversely. Besides it will also couple the lifecycle : you create a new version of the image of both applications while only one application may be changed.
Many serious drawbacks for a small advantages.

huangapple
  • 本文由 发表于 2020年7月26日 04:36:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/63093318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定