Published on

Multistage Spring Boot Docker Build and Overriding


Using Docker multi-stage builds it is quite easy to build your Spring fat jar in one container and run it in a much lighter container.

The dockerfile that does it is below:

FROM gradle:jdk11 AS builder
WORKDIR /home/root/build/
COPY . .
RUN gradle build -x test

FROM openjdk:11-jre-slim
WORKDIR /home/root/your-awesome-app/
COPY --from=builder /home/root/build/build/libs/gs-spring-boot-0.1.0.jar /home/root/your-awesome-app/
ENTRYPOINT ["java","-jar","gs-spring-boot-0.1.0.jar"]

I also use the following .dockerignore:


Overriding properties from your at startup time is quite easy. The java command above would look as follows if we wanted to change the port used by spring:

java -jar gs-spring-boot-0.1.0.jar --server.port=8081

Based on this we can update the last line of the dockerfile above to:

# ...
ENTRYPOINT ["java","-jar","gs-spring-boot-0.1.0.jar", "--server.port=8081"]

If you used docker-compose for dev locally and you tagged your image as foo:0.1 by running docker build -t foo:0.1 ., you could then override spring application properties in a compose file as follows:

version: "3"

    image: foo:0.1
      - "queue"
      - "8080:8080"
      - server.port=8081
      - mq.url=tcp://queue:61616
    image: rmohr/activemq
      - "61616:61616"
      - "8161:8161"

In the above, I change the port used by spring boot. I also overrode a property I had in my file called mq.url.

As a side note to refer to another container's IP in a docker-compose file you do so by referring to the service name. For example, in the above, I reference the queue service's docker hostname by using the service name which is a queue. i.e. the queue URL is: tcp://queue:61616.