- Published on
Multistage Spring Boot Docker Build and Overriding application.properties
- Authors
- Name
- Yair Mark
- @yairmark
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 /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
:
.git/
build/
.idea/
.settings/
.project
.classpath
Overriding properties from your application.properties
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"
services:
foo:
image: foo:0.1
depends_on:
- "queue"
ports:
- "8080:8080"
environment:
- server.port=8081
- mq.url=tcp://queue:61616
queue:
image: rmohr/activemq
ports:
- "61616:61616"
- "8161:8161"
In the above, I change the port used by spring boot. I also overrode a property I had in my application.properties
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
.