0

My aim is to add my java code and all jars in a container and run the code whenever I run my container . How do i achieve this?

Aditya
  • 3
  • 5

1 Answers1

0

disclaimer: I haven't been working with Windows containers, but the approach described should work still.

I've personally started to use multi-stage builds to achieve good results. With multi stage builds, you can use different image to build the artifacts, in this case a fat jar, and then copy it over to some other image used to run the application.

FROM  maven:3-jdk-8 as builder

WORKDIR /app

ADD pom.xml /app/

# To cache all the dependencies until pom.xml changes
# Does not work perfectly
RUN mvn dependency:go-offline dependency:resolve

ADD . /app/

RUN mvn package -D skipTests=true


FROM openjdk:8-slim

COPY --from=builder /app/target/my-app-0.0.1-SNAPSHOT.jar /app/app.jar

CMD ["java", "-jar", "/app/app.jar"]

In the sample app here, it's based on Spring Boot so it automatically creates a fat jar with maven package command. For your app you might need to setup some plugin to create the fat jar.

So the idea is to try to provide as slim as possible image to actually run the application, without any additional build-time tooling.

Numppa
  • 16
  • 1