0

I have a project that is built via maven, its a dockerized project for a node application.

I want to be able to customize my CMD/EntryPoint based on the maven build arguments.

I know that when we do docker run and provide it the arguments it is accepted and that works fine. but I want to do the same thing from maven commandline.

Is there a way to let docker run know the argument passed in the maven commandline? or even better can I edit the dockerfile and read commandline args of maven and use in the dockerfile ENTRYPOINT?

Thanks in advance, Minakshi

  • 1
    What are you actually trying to achieve? Sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Turing85 Aug 26 '20 at 19:31
  • lol, didn't know this has a term! but yea, I fall frequently into it, nevertheless, my problem is solved. \m/ – Minakshi Mishra Sep 04 '20 at 04:26

2 Answers2

0

Based on this, you can either use maven-resources-plugin to replace instances of ${...} with the values set in maven before you build the docker file.

Example:

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
    <execution>
        <id>filter-dockerfile</id>
        <phase>generate-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${project.build.directory}</outputDirectory>
            <resources>
                <resource>
                    <directory>src/main/docker</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>

This assume your docker file is under src/main/docker/ path. The replaced docker file will be copied on ${project.build.directory} path.

Or based on this comment, you could pass arguments to docker file.

Example:

On your maven docker plugin

<configuration>
  <buildArgs>
    <artifactId>${project.artifactId}</artifactId>
    <groupId>${project.groupId}</groupId>
  </buildArgs>
</configuration>

Then access those properties as ARGs on docker file

ARG artifactId
ARG groupId
ENV ARTIFACT_ID=${artifactId} GROUP_ID=${groupId}

Hope this help answer you question.

BadHumor
  • 76
  • 4
  • thank you for your response, yes I had found resource filtering as well as an option and tried it, that worked, I was at first looking for a way to just add a variable in my Entrypoint that could be interpolated from maven, but I guess that is hard to do. – Minakshi Mishra Aug 31 '20 at 16:49
  • I think the second example is what are you looking for. In that example you define arguments on maven docker build plugin and those are passed and are available to docker file during image build. In that example parameters```${project.artifactId}``` and ```${project.groupId}``` used that already known to maven, but you could set and pass values from your maven command or properties file [see](https://stackoverflow.com/a/24213229/14169282). I also tried the first example approach in the past but not the second one. – BadHumor Sep 03 '20 at 11:11
0

Thank you for the responses

I used the resource filtering in maven to solve my problem:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <userdefvariable></userdefvariable> // variable that you want to pass along
</properties>
<build>
  <resources>
    <resource>
      <directory>src/main/resource</directory> // path to the file (can be anything)
      <filtering>true</filtering> // must be true this is what does replacement
    </resource>
  </resources>
</build>

add to maven commands: "resources:resources -Duserdefvariable=value"

//this setup generates a file in target folder after running the mvn commands, where the variable is injected with the value given by the user.

in Dockerfile now you can instead put in a command to run the file: CMD ["sh", "path to the script in target folder"]

// in this script should be the commands that you want to use