When we build a new container image we usually start with an existing container image that already contains a minimum of the configuration we need, for example the OS, the Java version or even more. For this we search DockerHub or on the internet to find a starting point which fits to our needs.
Inside of our Dockerfile we use two stages to build the container image . The reason for the two stages is that we want to be independend of an existing local environment when we build our production services. With this concept we don't have to ensure that e.g. Java and Maven or correct versions of them are installed on the local machine of the developers.
With this two stage approach there is one container responsible to build the Microservice, let us call this container build environment container, and another container will contain the Microservice itself, we call this the production container. Only this production container is later used.
Build environment container
In the following Dockerfile sample we can see how we create our build environment container based on the maven 3.5 image from DockerHub.
We use the pom file that we defined before to build our Authors service with RUN mvn -f /usr/src/app/pom.xml clean package.