Java docker file. Build and test or just run jar file?

Viewed 3544

I'm thinking this in a CI pipeline where I should first build and test my app and the result should be a docker image.

I'm wondering if it's more common to build on the build server using the build environment and then running tests. Maybe using a build script for this. And lastly just add the jar file to the Docker container produced using COPY and then have Entrypoint java -jar .jar. So keep the Dockerfile very small, and have testing and building outside the container.

A bit like this:

FROM openjdk:8-jre-alpine
CMD java ${JAVA_OPTS} -jar *.jar
COPY build/libs/*.jar .

Or if I should add all the source code to the container, build it and then run tests inside the container and then having the Entrypoint (as before) running the jar file that was produced? So keeping everything in the Dockerfile? Maybe doing some cleanup also, removing the source code

This doesn't really have to be Java I guess, the same question applies to all languages

3 Answers

To solve this problem, I borrowed the SoC (Separation of Concerns) design principle from computer science and decided to go with the two docker image approach: one to build/test the app and one to run it. Another benefit of keeping the processes separate is reducing the chance of unintended behavior which will simplify maintenance and regression testing.

Related