Best way to run jar application with arguments from env variables

8/21/2019

I have a very simple java jar application that fetches DB properties from the env variables. I`ve dockerized it and migrated to Kubernetes. I also created a config map and secret with parameters to my DB - so I have access to these properties in the container. Is it possible to fetch all properties and inject it in Dockerfile? How can I do it?

FROM openjdk:8-jre
ADD target/shopfront-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8010
ENTRYPOINT ["java","-Ddb.host=**value-from-env-variable**","-jar","/app.jar"]
-- michf
java
kubernetes

3 Answers

8/21/2019

The array or "exec form" of entrypoint uses exec to run the specified binary rather than a shell. Without a shell the string $DB_HOST is passed to your program as argument.

Shell form

ENTRYPOINT java -Ddb.host="${DB_HOST}" -jar /app.jar

Shell script

If your startup get more complex you can also use an ENTRYPOINT script.

ENTRYPOINT ["/launch.sh"]

Then launch.sh contains:

#!/bin/sh -uex
java -Ddb.host="${DB_HOST}" -jar /app.jar
-- Matt
Source: StackOverflow

8/21/2019

As I understand you need to fetch parameters from config map and secret and set them as environment variables in your container. Furtunately it's quite nicely described in Kubernetes documentation.

Have a look at below links:

To sum up, such resources need to be just defined in Pod's configuration.

-- Adam Bukowiecki
Source: StackOverflow

8/21/2019

You can use them like this

ENTRYPOINT ["java", "-jar", "-Ddb.host=${DB_HOST}", "/app.jar"]

where DB_HOST should be defined in config map you have created.

I have tried this in my Spring Boot Application for setting Spring Profile.

-- tarun khosla
Source: StackOverflow