0

How can we run mvn commands from a Dockerfile

Here is my Dockerfile:

FROM maven:3.3.9-jdk-8-alpine
WORKDIR /app
COPY code /app
WORKDIR /app
ENTRYPOINT ["mvn"]
CMD ["clean test -Dsurefire.suiteXmlFiles=/app/abc.xml"]

I tried to build and run the above image and it fails ( abc.xml is under the /app directory)

Is there a way to get this to work.

Dilip
  • 365
  • 1
  • 6
  • 18
  • This helpful? https://stackoverflow.com/questions/27767264/how-to-dockerize-maven-project-and-how-many-ways-to-accomplish-it – triplem May 03 '21 at 14:47
  • you should use the 'mvn' command inside the entrypoint – fmdaboville May 03 '21 at 15:02
  • You probably want to `RUN mvn ...` as part of your image build sequence, not as the main container command. Set `CMD java -jar ...` to run the built application, and don't set `ENTRYPOINT` at all. If you use the JSON-array form of any of these commands, you need to break it into a separate word per array element. – David Maze May 03 '21 at 18:16
  • The main goal is to be able to clone a repo ( in this case via COPY command as i have the repo locally) and run an mvn clean test - via the ENTRYPOINT or/and CMD. And not using the RUN command. – Dilip May 04 '21 at 06:10
  • When i use - ENTRYPOINT ["mvn clean test -Dsurefire.suiteXmlFiles=/app/abc.xml"] , i get a no such file or directory error. – Dilip May 04 '21 at 06:23

1 Answers1

1

According to the documentation: "If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format." As such you should rewrite CMD as follow:

CMD ["clean","test","-Dsurefire.suiteXmlFiles=/app/abc.xml"]

You can also parameterize entry-point as a JSON array, as per documentation: ENTRYPOINT["mvn","clean","test","-Dsurefire.suiteXmlFiles=/app/abc.

But, I suggest you use best-practice with an entrypoint ash-file. This ensures that changing these parameters does not require rewriting of the dockerfile:

  1. create an entrypoint.sh file in the code directory. make it executable. It should read like this:

    #!/bin/sh if [ "$#" -ne 1 ] then FILE="abc.xml" else FILE=$1 fi mvn clean test -Dsurefire.suiteXmlFiles="/app/$FILE"

  2. replace your entrypoint with ENTRYPOINT["./entrypoint.sh"]

  3. replace your command with CMD["abc.xml"]

PS you have "WORKDIR /app" twice. this isn't what fails you, but it is redundant, you can get rid of it.