Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-maven-plugin: how do I pass environment variable from `docker run ... -e <value>` to build or run step?

I have a .jar that contains multiple public static void main(psvm)'s that I want to be able to call when I do docker run ... -e <class.path.from.env> on the image and pass an environment variable to specify the class path. Something like this:

  <plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <configuration>
      <images>
        <image>
          <name>${project.artifactId}</name>
          <build>
            <from>java:8-jre</from>
            <tags>
              <tag>${build.environment}-latest</tag>
              <tag>${build.environment}-${build.number}</tag>
            </tags>
            <entryPoint>
              <exec>
                <arg>java</arg>
                <arg>-Duser.timezone=UTC</arg>
                <arg>-cp</arg>
                <arg>/opt/${project.artifactId}-${project.version}.jar</arg>
                <arg>${class.path.from.env}</arg>
              </exec>
            </entryPoint>
            <assembly>
              <basedir>/opt</basedir>
              <inline>
                <files>
                  <file>
                    <source>target/${project.artifactId}-${project.version}.jar</source>
                  </file>
                </files>
              </inline>
            </assembly>
          </build>
        </image>
      </images>
    </configuration>
  </plugin>

Although I read the whole documentation for docker-maven-plugin, I'm not sure how I can make this work. Basically where do I declare the environment variable class.path.from.env and how can I make sure it gets the one I pass through -e in docker run ...?

like image 865
breezymri Avatar asked Oct 29 '22 03:10

breezymri


1 Answers

I think you need to declare a <run> section next to your <build> section, and add your env variable to <env>, as described here: https://dmp.fabric8.io/#misc-env

<run>
  <env>
    <CATALINA_OPTS>-Xmx32m</CATALINA_OPTS>
    <JOLOKIA_OFF/>
  </env>
like image 170
Ondra Žižka Avatar answered Nov 15 '22 07:11

Ondra Žižka