Search code examples
javadockerdocker-composedockerfiletomcat9

Is there any way to do java compilation and deployment to web server inside the docker container?


Is there any way to do java compilation and deployment to web server inside the docker container?

I'm not able to pull both JDK image and Tomcat Server images in single Dockerfile, is there any alternative to address this issue?

Thanks in advance


Solution

  • Sure, you have write a multistage docker file as explained here https://docs.docker.com/develop/develop-images/multistage-build/ and here https://mailslurp.medium.com/faster-java-containers-with-docker-multi-stage-builds-cc63e056e546 .

    Follow and example from the last postd link:

    # build stage build the jar with all our resources
    FROM openjdk:8-jdk as build
    
    ARG VERSION
    ARG JAR_PATH
    
    VOLUME /tmp
    WORKDIR /
    ADD . .
    
    RUN ./gradlew --stacktrace clean test build
    RUN mv /$JAR_PATH /app.jar
    
    # package stage
    FROM openjdk:8-jdk-alpine
    WORKDIR /
    # copy only the built jar and nothing else
    COPY --from=build /app.jar /
    
    ENV VERSION=$VERSIOsN
    ENV JAVA_OPTS=-Dspring.profiles.active=production
    
    EXPOSE 5000
    
    ENTRYPOINT ["sh","-c","java -jar -Dspring.profiles.active=production /app.jar"]
    

    Obviously you have to customized it as per your needs.