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

6

21

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.

Culet answered 29/6, 2019 at 1:3 Comment(1)
Have you tried using the environment variable SPRING_PROFILES_ACTIVE?Bortman
C
16

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
Covell answered 29/6, 2019 at 5:9 Comment(4)
Thanks, michalk and @Chrylis. The SPRING_PROFILES_ACTIVE solution did work. However, solution #1, setting $JAVA_OPTS did not work. I updated the docker file as you have mentioned, and then used the following command: docker run -p 8000:80 demo-app --rm -e JAVA_OPTS='-Dspring.profiles.active=dockerdev', but it could not pick up the JAVA_OPTS. Where am I going wrong?Culet
Solution 1 seems clean and can be easily updated to be dynamicMiscall
@Culet you should accept the answer or indicate if more help is needed.Copywriter
ENTRYPOINT ["java","-Dspring.profiles.active=${SPRING_PROFILE}","-jar","/opt/lib/demo-app.jar"] didn't work for me, it wouldn't expand the ${SPRING_PROFILE} until I got rid of the []Andaman
H
9

JAVA_TOOL_OPTIONS maybe the right answer.

docker run -p 8000:80 -e JAVA_TOOL_OPTIONS='-Dspring.profiles.active=dockerdev' demo-app
Hammers answered 28/1, 2021 at 11:48 Comment(0)
B
6

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.

Burkes answered 29/6, 2019 at 12:44 Comment(0)
L
3

People who are looking for the answer for non Spring (Plain Java Applications)

This is how to send System properties/arguments: Change the Dockerfile to

FROM openjdk:8-jdk-alpine
COPY target/demo-app-1.0-SNAPSHOT.jar demo-app.jar

EXPOSE 8080

ENTRYPOINT java -jar demo-app.jar

Build the image:

docker build -t demo-app .

Then run the docker container using following command:

docker run -e "JAVA_TOOL_OPTIONS=-Xms1024m -Xmx2048m -Dspring.profiles.active=dockerdev" -p 8080:8080 demo-app
Locke answered 24/7, 2020 at 22:45 Comment(0)
P
2

you have to provide the JAVA_OPTS inside the docker file, example of a docker file is below.

FROM {{ env "DOCKER_REGISTRY" }}/asdf/osX-jre8:{{ env "BASE_IMAGE_VERSION" }}

ADD target/yourapp.jar /app.jar

#Environment vars
ENV NO_PREFIX true
ENV APP_NAME "xxx"
ENV APP_UUID "81b35e09-2a10-48c3-a091-xxxxxxxxx"
ENV HEALTH_CHECK_URL http://localhost:9000/health
ENV SERVICE_PORT 8080

ENV JAVA_OPTS "-Dsun.net.client.defaultConnectTimeout=2000 -Dsun.net.client.defaultReadTimeout=20000 -XX:+PrintGC -XX:+PrintGCTimeStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/alloc/logs"
Playacting answered 29/6, 2019 at 1:13 Comment(1)
Sorry, as clarified above, I don't want to mention this in the docker file. Else I would need a separate docker file for each environment I am running it inCulet
M
1

You should add the env variable JAVA_OPTS to your Dockerfile

FROM tomcat:8.5.47-jdk8-openjdk

LABEL build_date="2020-07-14" \
      name="Ousama EL IDRISSI" \
      version="1.0" \
      description="Docker Image made by la7ya"

EXPOSE 8080

COPY ./target/la7yaman-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war

ENV JAVA_OPTS="-Dspring.profiles.active=docker-demo"

CMD ["catalina.sh", "run"]
Maxwell answered 14/7, 2020 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.