简体   繁体   English

Docker –在docker容器中编译maven项目并从那里运行

[英]Docker – Compile maven project inside docker container & run it from there

I have a project running Vue & Spring Boot that I need to create a docker-compose.yml file to run mvn clean install to generate the .jar , and then build a "new" image from another Dockerfile with that said .jar inside the docker container. 我跑的Vue和春季启动,我需要创建项目docker-compose.yml文件运行mvn clean install生成.jar ,然后从另一个Dockerfile打造一个“新”的形象与说.jar内泊坞窗容器。

This is the Dockerfile that needs to be run once the mvn clean install is completed: 这是在mvn全新mvn clean install完成后需要运行的Dockerfile:

FROM java:8

ENV WKHTML_VERSION 0.12.4

# Builds the wkhtmltopdf download URL based on version numbers above
ENV DOWNLOAD_URL "https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTML_VERSION}/wkhtmltox-${WKHTML_VERSION}_linux-generic-amd64.tar.xz"

RUN apt-get update && \
    apt-get install -y --no-install-recommends wget && \
    wget $DOWNLOAD_URL && \
    tar vxf wkhtmltox-${WKHTML_VERSION}_linux-generic-amd64.tar.xz && \
    cp wkhtmltox/bin/wk* /usr/local/bin/ && \
    cp wkhtmltox/lib/* /usr/local/lib/ && \
    rm wkhtmltox-${WKHTML_VERSION}_linux-generic-amd64.tar.xz

# @see https://spring.io/guides/gs/spring-boot-docker/
COPY   server/target/redo-server-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

So the build steps need to be something like this: 因此,构建步骤必须是这样的:

  • Install node + maven / pull those images 安装节点+ Maven /拉那些图像
  • Install postgresql & pull that image 安装postgresql并提取该图像
  • Run mvn clean install & generate .jar 运行mvn clean install并生成.jar
  • Build new image from abovementioned Dockerfile and run it 从上述Dockerfile构建新映像并运行它

I am new to docker-compose so I am having troubles setting this up in the correct execution order. 我是docker-compose新手,因此无法按正确的执行顺序进行设置。

The reason I need to do this is due to a problem with the production pipeline not having node or npm , which is needed to run the full maven application ( Vue.js and Spring Boot app), which is why it needs to be compiled from inside the Docker container 我需要执行此操作的原因是由于生产管道中没有node or npm ,而运行完整的maven应用程序( Vue.jsSpring Boot应用程序)则需要使用node or npm ,这就是为什么需要从中进行编译的原因在Docker容器中

It would be greatly appreciated if anyone could point me in the right direction, let alone – is this possible to do? 如果有人能指出正确的方向,我将不胜感激,更不用说–这可能吗?

Solved by writing a multi-step build as my Dockerfile. 通过编写一个多步骤构建作为我的Dockerfile来解决。 I am installing node as a dependency in the client's pom.xml file. 我正在将node作为依赖项安装在客户端的pom.xml文件中。

# Install maven and copy project for compilation
FROM maven:latest as builder

COPY pom.xml /usr/local/pom.xml
COPY server /usr/local/server
COPY client /usr/local/client
WORKDIR /usr/local/

RUN mvn clean install


FROM openjdk:8
ENV WKHTML_VERSION 0.12.4
# Builds the wkhtmltopdf download URL based on version numbers above
ENV DOWNLOAD_URL "https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTML_VERSION}/wkhtmltox-${WKHTML_VERSION}_linux-generic-amd64.tar.xz"

RUN apt-get update && \
    apt-get install -y --no-install-recommends wget && \
    wget $DOWNLOAD_URL && \
    tar vxf wkhtmltox-${WKHTML_VERSION}_linux-generic-amd64.tar.xz && \
    cp wkhtmltox/bin/wk* /usr/local/bin/ && \
    cp wkhtmltox/lib/* /usr/local/lib/ && \
    rm wkhtmltox-${WKHTML_VERSION}_linux-generic-amd64.tar.xz

COPY   --from=builder /usr/local/redo/server/target/server-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM