简体   繁体   English

Maven + Java 应用在 Dockerfile

[英]Maven + Java application in Dockerfile

I am new to java and maven.我是 java 和 maven 的新手。 I have built an application that executes a flink job.我已经构建了一个执行 flink 作业的应用程序。 I have created a base docker image but I am not sure how to excecute/run like I run the application in the terminal.我已经创建了一个基本的 docker 图像,但我不确定如何像在终端中运行应用程序一样执行/运行。

I currently run the application in the terminal as follows:我目前在终端中运行应用程序如下:

 mvn package exec:java `-D exec.args="--runner=FlinkRunner --flinkMaster=localhost:8081 --filesToStage=.\target\maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar `" -P flink-runner`

Here is my docker file这是我的 docker 文件

FROM maven:latest AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package

FROM openjdk:14
COPY --from=build /usr/src/app/target/maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar /usr/app/maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar
WORKDIR /usr/app
EXPOSE 8080
ENTRYPOINT ["java","-jar","maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar"]

Any suggestions?有什么建议么?

Thanks in advance!提前致谢!

You are running your app with a maven plugin and with a maven profile.您正在使用 maven 插件和 maven 配置文件运行您的应用程序。 You need your app to be runnable outside of maven first.您首先需要您的应用程序可以在 maven 之外运行。

Then, you need to cleanup your docker steps a bit, here are some suggestions:然后,您需要稍微清理一下您的 docker 步骤,这里有一些建议:

  • Move copy src after coyping pom and downloading dependencies复制 pom 并下载依赖项后移动复制 src
  • Do not use root user for runnable image不要将 root 用户用于可运行映像
  • Use slimer base image for runnable image使用更苗条的基础镜像作为可运行镜像
  • Use exploded jars instead of fat jars to get slimmer layers使用爆款 jars 代替肥款 jars 以获得更纤薄的层次
  • Make use of.dockerignore to prevent copying unwanted things to the image利用 .dockerignore 防止将不需要的东西复制到镜像中

Here is a sample Dockerfile:这是一个示例 Dockerfile:

FROM maven:3.6.3-openjdk-14-slim AS build
WORKDIR /build
# copy just pom.xml (dependencies and dowload them all for offline access later - cache layer)
COPY pom.xml .
RUN mvn dependency:go-offline -B
# copy source files and compile them (.dockerignore should handle what to copy)
COPY . .
RUN mvn package
# Explode fat runnable JARS
ARG DEPENDENCY=/build/target/dependency
RUN mkdir -p ${DEPENDENCY} && (cd ${DEPENDENCY}; jar -xf ../*.jar)


# Runnable image
FROM openjdk:14-alpine as runnable
VOLUME /tmp
VOLUME /logs
ARG DEPENDENCY=/build/target/dependency
# Create User&Group to not run docker images with root user
RUN addgroup -S awesome && adduser -S awesome -G awesome
USER awesome

# Copy libraries & meta-info & classes
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
# Run application
ENTRYPOINT ["java","-cp","app:app/lib/*","com.myawesomeness.Application"]

Then your app, must be runnable outside of maven first.然后,您的应用程序必须首先在 maven 之外运行

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

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