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

时间:2019-03-13 10:18:37

标签: java maven spring-boot docker

我有一个运行Vue和Spring Boot的项目,我需要创建一个docker-compose.yml文件来运行mvn clean install来生成.jar,然后从另一个文件构建一个“新”映像在Docker容器内显示.jar的Dockerfile。

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"]

因此构建步骤必须是这样的:

  • 安装节点+ maven /拉取这些映像
  • 安装postgresql并提取该图像
  • 运行mvn clean install并生成.jar
  • 从上述Dockerfile构建新图像并运行

我是docker-compose的新手,因此我无法按正确的执行顺序进行设置。

之所以需要执行此操作,是因为生产管道没有node or npm,这是运行完整的maven应用程序(Vue.jsSpring Boot应用程序所必需的) ,这就是为什么需要从Docker容器内部进行编译的原因

如果有人能指出我正确的方向,将不胜感激–这可能吗?

1 个答案:

答案 0 :(得分:0)

通过编写多步骤构建作为我的Dockerfile解决。我正在将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"]