spring-bootkubernetesenvironment-variablesminikubecontextpath

Start sprinboot in minkube with custom context url as environnement variable


I have a springboot app which I want to deploy on Kubernetes (I'm using minikube) with a custom context path taken from the environment variables.

I've compiled an app.war file. exported an environment variable in Linux as follow:

export SERVER_SERVLET_CONTEXT_PATH=/app

And then started my app on my machine as follow:

java -jar app.war --server.servlet.context-path=$(printenv CONTEXT_PATH)

and it works as expected, I can access my app throw browser using the url localhost:8080/app/

I want to achieve the same thing on minikube so I prepared those config files:



However, the java container inside the pod fails to start and here's the exception is thrown by spring:

Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'


Solution

  • Finally, I found a solution.

    I configured my application to startup with a value for the context path taken from the environment variables by adding this line inside my application.properties:

    server.servlet.context-path=${ESSE_APPLICATION_CONTEXT}
    

    And the rest remains as it was, means I'm giving the value of the variable ESSE_APPLICATION_CONTEXT throw the config

    env:
    - name: ESSE_APPLICATION_CONTEXT
      value: /esse-1
    

    And then starting the application without the --server.servlet.context-path parameeter, which means like that:

    java -jar app.war
    

    NOTE: as pointed by @mchawre's answer, it's also possible to make use of ConfigMap as documented in Kubernetes docs.