Search code examples
javadockermavendockerfile

To include maven in dockerfile or not?


I have this working simple Dockerfile.

FROM openjdk:8-jdk-alpine
WORKDIR /data
COPY target/*.jar, myapp.jar
ENTRYPOINT ["java","-jar",myapp.jar]

I build my jar using maven either locally or in a pipeline then use that .jar here. I've seen many examples installing maven in the Dockerfile instead of doing the build before. Doesn't that just make the image larger? Is there a benefit of doing that?


Solution

  • Usually I have a CI/CD server which I use for building my jar file and then I generate a docker image using it. Build a jar consumes resources and doing it when you're running your docker container can take longer depending on your configuration. In a normal CI/CD strategy, build and deploy are different steps. I also believe your docker image should be as lean as possible.