Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda