2

I have a spring boot application that is dockerized. By default the application has spring.cloud.config.enabled=false hence the application doesn't pick up the application.properties from configserver. However while deploying in a separate env we need to integrate the application with configserver so we need to override and set the spring.cloud.config.enabled property to true.

To achieve this I am running the docker image using the following docker-compose file :

version: '3'

services:
  my-app-new:
    container_name: my-app
    image: my-app:1.0.0-SNAPSHOT
    ports:
      - "8070:8070"
    environment:
      - SPRING_CLOUD_CONFIG_ENABLED=true
      - SPRING_CLOUD_CONFIG_URI=http://localhost:14180

However, it just doesn't work. If I hard code the values in the property file then it integrates fine.

I also tried the following command but it still didn't work :

docker run -p 8070:8070 -e SPRING_CLOUD_CONFIG_ENABLED=true -e SPRING_CLOUD_CONFIG_URI=http://localhost:14180 my-app:1.0.0-SNAPSHOT

The spring boot version is 2.2.6.

Let me know what the problem is.

Update : I cannot use profiles as there too many env in our company and even the VMs keep getting changed so cannot have hardcoded profiles. We want a solution where we can just pass certain variables from the outside.

As someone pointed out in the comments the above compose yml is not working as the environment variables need to read by the spring boot application. So did some research on the internet and instead we are now passing the JAVA_OPTS tomcat variable while running the image. Like so :

docker run --env JAVA_OPTS="-Dspring.cloud.config.uri=http://localhost:14180 -Dspring.cloud.config.enabled=true" -p 8080:8080 my-app-image

And in the docker file we have used the JAVA_OPTS while starting the jar like so

ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar" ]

And this still doesnt work. Not sure what is going wrong.

humbleCoder
  • 667
  • 14
  • 27
  • 1
    You are setting these variables in Docker environment. You need to read these values from environment in spring boot application. And once the application is started I believe this property cannot be changed. Check this https://stackoverflow.com/questions/35531661/using-env-variable-in-spring-boots-application-properties – Tushar Jajodia Sep 04 '20 at 04:58
  • 1
    My understanding is application.properties will be read regardless the environment variable. The environment variable only overrrided them. I ur case u will need to use profile. Let say u have application-cloud.properties. The environment variable SPRING_PROFILES_ACTIVE=cloud – Chun Sep 04 '20 at 05:17
  • Please check my updated question. We cannot have profiles, we want a solution where we can pass certain properties while we start the application like we can do when starting the spring boot app jar from the command like. – humbleCoder Sep 05 '20 at 15:25
  • Do you have config set to use the env variables [`spring.cloud.config.enabled = ${SPRING_CLOUD_CONFIG_ENABLED}`](https://stackoverflow.com/a/35535138/5521670)? – Šimon Kocúrek Sep 05 '20 at 15:46
  • @ŠimonKocúrek: can you please check the updated part of the question. – humbleCoder Sep 05 '20 at 15:49
  • @humbleCoder Soooo you don't. Try setting it. – Šimon Kocúrek Sep 05 '20 at 15:52

2 Answers2

1

I found the problem with my setup. I made a silly error. The config server is not in my docker network and I used localhost to communicate with the config server. Localhost would of course mean that I am referring to the app containers IP which only has the app running. Instead when I used the ip address or the hostname of my machine my application container could connect to the config server successfully.

humbleCoder
  • 667
  • 14
  • 27
-2

Why you not run container --> go inside --> change configuration and commit to new images. After that deploy to new env.

Vinamit_
  • 73
  • 4