简体   繁体   English

使用入口点和/或 CMD 运行 mvn 命令作为 docker 文件的一部分

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

How can we run mvn commands from a Dockerfile我们如何从 Dockerfile 运行 mvn 命令

Here is my Dockerfile:这是我的 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)我尝试构建并运行上面的图像,但它失败了( abc.xml 在 /app 目录下)

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." 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 如下:

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.您还可以根据文档将入口点参数化为 JSON 数组:ENTRYPOINT["mvn","clean","test","-Dsurefire.suiteXmlFiles=/app/abc。

But, I suggest you use best-practice with an entrypoint ash-file.但是,我建议您使用带有入口点 ash 文件的最佳实践。 This ensures that changing these parameters does not require rewriting of the dockerfile:这确保更改这些参数不需要重写 dockerfile:

  1. create an entrypoint.sh file in the code directory.在代码目录中创建一个 entrypoint.sh 文件。 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" #./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"]用 ENTRYPOINT["./entrypoint.sh"] 替换你的入口点

  3. replace your command with CMD["abc.xml"]用 CMD["abc.xml"] 替换你的命令

PS you have "WORKDIR /app" twice. PS你有两次“WORKDIR /app”。 this isn't what fails you, but it is redundant, you can get rid of it.这不是你失败的原因,但它是多余的,你可以摆脱它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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