Docker多阶段、Java简单类

cidc1ykv  于 2022-12-11  发布在  Docker
关注(0)|答案(1)|浏览(160)

在Docker中创建多程序集时遇到问题。我收到此错误,错误为jarfile无效或损坏

FROM openjdk:18-jdk-alpine AS build
ARG JAR_FILE
COPY ${JAR_FILE} /app/app.jar
COPY . /app/source
WORKDIR /app/source
EXPOSE 8001

FROM openjdk:18-jdk-alpine AS runtime
COPY --from=build /app/source/ /app/app.jar
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
ctehm74n

ctehm74n1#

  1. Before java run execute jar, you need create a execute jar.
  2. In src, create a file name manifest.mf
    Manifest-version: 1.0
    Main-Class: Main
  3. Dockerfile
    FROM eclipse-temurin:17.0.5_8-jdk-ubi9-minimal AS build
    WORKDIR /tmp/app
    COPY src /tmp/app/src
    RUN mkdir -p /tmp/out
    RUN javac -d /tmp/out src/.java
    RUN cp /tmp/app/src/manifest.mf /tmp/out/manifest.mf
    RUN ls /tmp/out/

    RUN cd /tmp/out/ && jar cfm /tmp/app/app.jar manifest.mf Main.class Player.class VARIANTS.class
    FROM eclipse-temurin:17.0.5_8-jre-ubi9-minimal
    WORKDIR /APP
    COPY --from=build /tmp/app/app.jar /APP/app.jar
    ENTRYPOINT ["java", "-jar", "app.jar"]
  4. build command
    docker build -t taskone:1.0.0 . docker tag taskone:1.0.0 taskone:latest
  5. run command
    docker run -it taskone
  6. check image command
    docker images | grep taskone
  7. Package class to jar
    jar cfm /tmp/app/app.jar manifest.mf Main.class Player.class VARIANTS.class
  8. If you package your class to jar in host, you don't need multi stage build. Just copy host jar file.
    Dockerfile
FROM eclipse-temurin:17.0.5_8-jre-ubi9-minimal
WORKDIR /APP
COPY app.jar /APP/app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

I already test OK in my machine.

相关问题