简体   繁体   中英

Run mvn commands as part of the docker file using Entrypoint and/or CMD

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM