Docker-compose - 使用重用 maven 存储库的 maven 构建 [英] Docker-compose - build with maven that re-uses the maven repository

查看:43
本文介绍了Docker-compose - 使用重用 maven 存储库的 maven 构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Maven 构建我的 Spring-boot 映像时,我现在在 Dockerfile 中执行此操作.Maven 将下载所有依赖项,然后编译我的文件.这需要相当长的时间.

When building my Spring-boot image using Maven I now do this inside the Dockerfile. Maven will download all dependencies and then compile my files. This takes quite some time.

如何指定通过 docker-compose (Dockerfile) 的构建过程重新使用我的Windows10 Home"Maven 存储库?因此,(新)下载的数量很少.我的开发环境:我使用 Docker 快速入门终端,因此使用 docker-machine.

How can I specify that the build process via docker-compose (Dockerfile) re-uses my "Windows10 Home" Maven repository? So, the number of (new) downloads is minimal. My dev context: I use the Docker quickstart terminal, so using a docker-machine.

这是我的 docker-compose.yml 文件的一部分:

The is is a part of my docker-compose.yml file:

version: '3'
services:
  spring-boot-app:
    image: spring-boot-image
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - mysql-docker-container
    ports:
      - 8087:8080
    volumes:
      - $HOME/.m2/repository:/root/.m2/repository
      - /data/spring-boot-app

我的 Dockerfile 是:

My Dockerfile is:

FROM java:8
FROM maven:alpine
WORKDIR /app
COPY . /app
RUN mvn -v
RUN mvn clean install -DskipTests
EXPOSE 8080
LABEL maintainer="xyz@holland.nl"
ADD ./target/spring-boot-example-0.0.1-SNAPSHOT.jar /developments/
ENTRYPOINT ["java","-jar","/developments/spring-boot-example-0.0.1-SNAPSHOT.jar"]

推荐答案

@Jack Gore - 非常感谢您为我指明方向.我看过那个帖子,但还没有明确的答案.在深入研究所有单独的答案之后,这些信息为我提供了症状的答案以及如何解决类似问题的见解.

@Jack Gore - thank you very much for pointing me the way. I had seen the post, but there was not a definitive answer yet. After diving in all seperate answers, the info provided me both an answer to the symptom as well with insights how to solve similar questions.

ANSWER:通过 Dockerfile,您可以通过 1 个或多个后续镜像层构建最终镜像.为了防止该步骤每次都重新下载依赖,可以为依赖的下载世界"制作一个镜像层.只有在 pom.xml 文件中的依赖项更改时才会重新下载.

ANSWER: Via the Dockerfile you can build the final images via 1 or more subsequent image layers. To prevent the step from re-downloading the dependencies each time, you can make an image layer for the 'downloading the world' of dependencies. Re-downloading will only be done when a dependency changes in the pom.xml file.

为此,您将 Dockerfile 拆分为 2 个构建步骤:首先复制 pom.xml 文件并构建它.这将创建一个具有所有依赖项的图像层.第二步是构建应用程序.

To do so, you split the Dockerfile in 2 build steps: first copy the pom.xml file and build it. That will create an image layer with all dependencies. As a second step you build the application.

FROM java:8
FROM maven:alpine

# image layer
WORKDIR /app
ADD pom.xml /app
RUN mvn verify clean --fail-never

# Image layer: with the application
COPY . /app
RUN mvn -v
RUN mvn clean install -DskipTests
EXPOSE 8080
ADD ./target/your.jar /developments/
ENTRYPOINT ["java","-jar","/developments/your.jar"]

然后你会得到以下构建场景:

Then you get the following build scenario's:

  • 第一次您构建此 (docker build).下载依赖项,并在第 2 步构建应用程序 jar.
  • 当您立即重建依赖项 (pom.xml) 和应用程序源时,并没有改变.因此,不需要更改图像层.构建很快就准备好了.
  • 如果您更改了 1 个应用程序源文件,则只会下载少量下载并构建应用程序.所以你不是在下载世界.
  • 如果更改 pom.xml 文件,从而更改依赖项,则所有依赖项下载都已完成.
  • The first time you build this (docker build .) the dependencies are downloaded and as step 2 the application jar is build.
  • When you rebuild immediately the dependencies (pom.xml) and the application sources were not changed. So, the image layers don't need to be changed. The build is ready in no time.
  • If you change 1 of your application source files, only a few downloads are downloaded and the application is build. So you are NOT downloading the world.
  • If you change the pom.xml file, thus changing the dependencies, then all dependency downloads are done.

分离图像层的影响通过数字显示非常短的视频.

The impact of seperating image layers is shown via a number of very short videos.

这种构建方式的缺点当然是最终(生产)图像包含的不仅仅是应用程序.里面不仅有 JAR,还有一些依赖.

The disadvantage of this way of building is of course that the final (production) image contains more than the appication. Not only the JAR is in it, but also a number of dependencies.

如何解决这个太大的图像:

How to solve this iamge being far too big:

  • 通过技巧减小图像大小.使用选项:--squash.这在这篇文章中进行了解释.
  • 将构建过程(此处:maven 和/或 ng build --prod)作为 docker 构建步骤之前的一个步骤分开.然后执行 docker build 并仅将 jar 文件放入最终映像中.
  • 使用 CI/CD 环境,例如一个 Jenkinsfile(管道).在 Jenkins 管道中,您首先构建映像.所有依赖项都已经存在.您只需重建应用程序.您执行 docker 构建等.

就我而言,这是可能的最佳选择.您可以自动化该过程并保持较低的图像大小.

For my case this is the best option possible. You automate the process AND keep the image size low.

这篇关于Docker-compose - 使用重用 maven 存储库的 maven 构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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