Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass environment value defined in Kubernetes yaml config to java spring profile

I have the following in my Dockerfile to run my Springboot app:

ENTRYPOINT [ "java", "-jar", "/code/myapp/target/myapp.jar", "--spring.profiles.active=$ENV_PROFILE"]

I defined my environment variable (ENV_PROFILE) in my k8 YAML config as:

 spec:
  containers:
    - name: myspringboot
      image: myrepo/myapp:latest
      imagePullPolicy: Always
      resources:
        requests:
          cpu: 100m
          memory: 100Mi
      env:
      - name: ENV_PROFILE
        value: "test"

However, the environment name is not getting injected into the java springboot app. It shows as "$ENV_PROFILE" in the log. I also tried with application.properties by adding spring.profiles.active=$ENV_PROFILE and that does not work either.

like image 876
Katlock Avatar asked Apr 19 '19 12:04

Katlock


1 Answers

You can also override the Spring properties (and other properties defined in the application properties) by default, without specifying what environment variable to pass in your properties file. Environment variables have higher precedence then the properties file values.

See also Spring Externalized Configuration.

For example:

 spec:
  containers:
    - name: myspringboot
      image: myrepo/myapp:latest
      imagePullPolicy: Always
      resources:
        requests:
          cpu: 100m
          memory: 100Mi
      env:
      - name: SPRING_PROFILES_ACTIVE
        value: "test"
like image 184
Blokje5 Avatar answered Oct 19 '22 19:10

Blokje5