Docker-compose-使用Maven进行构建以重新使用Maven存储库 [英] Docker-compose - build with maven that re-uses the maven repository

查看:500
本文介绍了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分为两个构建步骤:首先复制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:

  • 首次构建(泊坞窗构建).将下载依赖项,并在步骤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.

通过非常短的视频数量.

这种构建方式的缺点当然是最终(生产)图像所包含的内容不止于应用.不仅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. 这篇文章中对此进行了解释.
  • 在Docker构建步骤之前,将构建过程(此处为:maven和/或ng build --prod)分开.然后执行docker build并仅将jar文件放在最终映像中.
  • 使用CI/CD环境,例如Jenkinsfile(管道).在Jenkins管道中,您首先构建映像.所有依赖项都已经存在.您仅重建应用程序.您将执行docker build等.
  • Reduce the image size via a trick. Use the option: --squash. This is explained in this post.
  • Seperate the build process (here: maven and/or ng build --prod) as a step before the docker build step. Then execute the docker build and put ONLY the jar file in final image.
  • Use an CI/CD environment with e.g. a Jenkinsfile (pipeline). In the Jenkins pipeline you first build the image. All dependencies are already there. You only rebuild the application. The you perform a docker build, etc.

对于我来说,这是最好的选择.您可以自动执行该过程,并保持较小的图像尺寸.

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天全站免登陆