Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

JAVA_TOOL_OPTIONS maybe the right answer.

docker run -p 8000:80 -e JAVA_TOOL_OPTIONS='-Dspring.profiles.active=dockerdev' demo-app
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!