Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Java options/variables to Springboot app in docker run command

I have a Spring Boot application which uses profiles to configure in different environments. I want to pass this profile information as a parameter to my docker run command. How do I go about doing it?

Here is my dockerfile

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/demo-app-1.0-SNAPSHOT.jar

COPY ${JAR_FILE} /opt/lib/demo-app.jar

EXPOSE 80

# ENTRYPOINT ["java","-Dspring.profiles.active=dockerdev","-jar","/opt/lib/demo-app.jar"]
# Above line works, but not desired as profile will change in diff envs
ENTRYPOINT ["java","-jar","/opt/lib/demo-app.jar"]

I have tried the following, but, none works

docker run -p 8000:80 demo-app -Dspring.profiles.active=dockerdev

docker run -p 8000:80 demo-app --rm -e JAVA_OPTS='-Dspring.profiles.active=dockerdev'

Please help.

Clarification: I am using multiple profiles. Hence I do not want the active profile to be mentioned within the application or the docker file. Instead, I want to use the same application and docker file and run it in different environments, and pass the active profile to be used in the docker run command. Apologies if anything above did not clarify that.

like image 990
TechiRik Avatar asked Jun 29 '19 01:06

TechiRik


People also ask

How do I pass a spring profile in Docker run?

Passing Spring Profile in Docker run You can also pass spring profile as an environment variable while using docker run command using the -e flag. The option -e “SPRING_PROFILES_ACTIVE=dev” will inject the dev profile to the Docker container.

How do I run spring boot from Docker?

This Dockerfile is very simple, but it is all you need to run a Spring Boot app with no frills: just Java and a JAR file. The build creates a spring user and a spring group to run the application. It is then copied (by the COPY command) the project JAR file into the container as app.


3 Answers

Solution 1

You can override any property from your configuration by passing it to docker container using -e option. As explained in Externalized configuration the environment variable name should be uppercased and splitted using underscore. So for example to pass spring.profiles.active property you could use SPRING_PROFILES_ACTIVE environment variable during container run :

docker run -p 8000:80 -e SPRING_PROFILES_ACTIVE=dockerdev demo-app

And this variable should be picked automatically by Spring from environment.

Solution 2

Change Dockerfile to :

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/demo-app-1.0-SNAPSHOT.jar

# environment variable with default value
ENV SPRING_PROFILE=dev

COPY ${JAR_FILE} /opt/lib/demo-app.jar

EXPOSE 80

#run with environment variable
ENTRYPOINT java -Dspring.profiles.active=$SPRING_PROFILE jar /opt/lib/demo-app.jar

and then run the container passing the environment variable :

docker run -p 8000:80 --rm -e SPRING_PROFILE=dockerdev demo-app
like image 169
Michał Krzywański Avatar answered Oct 31 '22 03:10

Michał Krzywański


Make use of application.properties in springboot to override any variables from outside. We heavily use this in our production environments.

You need to:

  • Change your ENTRYPOINT to:
ENTRYPOINT ["java","-jar","/opt/lib/demo-app.jar","--spring.config.additional-location=/application.properties"]
  • Create application.properties file with contents:
spring.profiles.active=dockerdev

You can also override any variables used in your springboot code using application.properties and can also override springboot specific variables as mentioned here.

  • Also change your docker run command to:
docker run -itd -v /path/to/application.properties:/application.properties image-name

So that application.properties from your host will get mounted inside your docker container.

NOTE: If --spring.config.additional-location don't works then try --spring.config.location option.

Hope this helps.

like image 22
mchawre Avatar answered Oct 31 '22 01:10

mchawre


JAVA_TOOL_OPTIONS maybe the right answer.

docker run -p 8000:80 -e JAVA_TOOL_OPTIONS='-Dspring.profiles.active=dockerdev' demo-app
like image 40
Peng Zhu Avatar answered Oct 31 '22 03:10

Peng Zhu