1

I'm using docker-maven-plugin and as per the documentation I need to pass environment variable file via <envPropertyFile>. So the plugin in pom.xml looks like

<configuration>
  <images>
    <image>
      <build>
        ...
      </build>
      <run>
         <envPropertyFile>${project.basedir}/local/local.properties</envPropertyFile>
      </run>
    </image>
  </images>
</configuration>

I have local.properties with below values,

TIME_COUNT=1000 
REST=10

In my java project I read those values as System.getenv("TIME_COUNT"); #which returns null.

Troubleshooting:
1.When I check env inside the container I see TIME_COUNT=1000 and REST=10.

docker exec -it CONTAINER_ID bash
env

2.When I execute

docker inspect -f '{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' CONTAINER_ID

I see all env values ( i.e TIME_COUNT=1000, REST=10 )

3.In my java when I tried to retrieve all the env I get none of env values from local.properties or the default which I could see inside the container by doing env.

StringBuilder sb = new StringBuilder();
Map<String, String> env = System.getenv();
  for (String key : env.keySet()) {
      sb.append(key + ": " + env.get(key)  + "\n");
  }
System.out.println(sb.toString());

4.I also tried passing env variable as mentioned below, which overwrote the value in container but the jar file still throwing null.

docker exec -e TIME_COUNT=12 -it CONTAINER_ID bash
Mad-D
  • 4,479
  • 18
  • 52
  • 93
  • My guess is that you need to pass those variables to your Java program using "-D" option. Something like this probably: ENTRYPOINT [ "java","-DTIME_COUNT=${env.TIME_COUNT}","-jar","yourjar.jar" ] – tsolakp Jan 08 '18 at 19:54
  • but the container does have all the `env` property. Why do I have to pass it again ? – Mad-D Jan 08 '18 at 20:14
  • Yes, but JVM most likely wont see unless you pass them from command line. – tsolakp Jan 08 '18 at 20:23
  • `-DTIME_COUNT=${env.TIME_COUNT}` or `-DTIME_COUNT=$TIME_COUNT` prints null. but if I pass hardcoded value it works fine. – Mad-D Jan 08 '18 at 22:02
  • @tsolakp That is not environment variables, but system properties. Similar but not the same. – Thorbjørn Ravn Andersen Jan 15 '18 at 09:06
  • @Thorbjørn Ravn Andersen. The env variables we're mentioned within Docker context and not Java. – tsolakp Jan 15 '18 at 17:48

1 Answers1

0

The cron loads with it's own environment variables and the workaround was to load the env from container to a file, then export that file before calling jar file.

Dockerfile:

...
#call start.sh to environment variable for cron
CMD /bin/bash /opt/project_dump/start.sh

start.sh:

# export all environment variables but `no_proxy` to use in cron.
# Note: no_proxy throws error while exporting due to formatting, envs.sh is created with export environment variables.
env | sed '/no_proxy/d' | sed 's/^\(.*\)$/export \1/g' > /root/envs.sh
chmod +x /root/envs.sh

# Run the cron command on container startup in foreground.
# Note: -n (foreground) keeps the container running instead of exiting once the crond is ran.
crond -n

execute.sh:

#!/bin/bash
/usr/lib/jvm/default-java/bin/java -jar /opt/project_dump/project_name.jar
#emptyline

crontab.txt:

#!/bin/bash
#calls environment script, execute script to call jar
*  *  * * *  root . /root/envs.sh;/opt/project_dump/execute.sh
#emptyline

Reference:
1. https://github.com/citta-lab/docker/tree/master/dockerCron
2. http://dev.im-bot.com/docker-cron/

Mad-D
  • 4,479
  • 18
  • 52
  • 93