spring-boot - 具有参数化的Dockerfile:失败并显示XXX

标签 spring-boot docker maven

我在一个简单的Spring Boot应用程序中有以下Dockerfile:

FROM maven:3.6-jdk-8-alpine as build

WORKDIR /app

COPY ./pom.xml ./pom.xml

RUN mvn dependency:go-offline -B

# copy your other files
COPY ./src ./src

# build for release
RUN mvn package -DskipTests

FROM openjdk:8-jre-alpine
ARG artifactid
ARG version
ENV artifact ${artifactid}-${version}.jar
WORKDIR /app

COPY --from=build /app/target/${artifact} /app

EXPOSE 8080

ENTRYPOINT ["sh", "-c"]

CMD ["java","-jar ${artifact}"]
当我使用必需的参数构建它时:
docker build --build-arg artifactid=spring-demo --build-arg version=0.0.1 -t spring-demo .
它建立没有错误。
当我尝试使用以下命令运行图像时:
docker container run -it spring-demo
它失败,并显示以下错误:
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32      use a 32-bit data model if available
    -d64      use a 64-bit data model if available
    -server   to select the "server" VM
                  The default VM is server,
                  because you are running on a server-class machine.


    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose:[class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
请问以上设置有什么问题?
该应用程序示例代码可以在here中找到。

最佳答案

您应该删除该ENTRYPOINT行,并使用CMD的 shell 形式。

# No ENTRYPOINT
CMD java -jar ${artifact}
Dockerfile的ENTRYPOINTCMDget combined into a single command line。在您的Dockerfile中,这被解释为
sh -c java '-jar ${artifact}'
但是sh -c选项实际上仅接收下一个单词并将其解释为要运行的命令;所以真的被处理为
sh -c 'java' # '-jar ${artifact}'
忽略-jar选项。
twoways来“拼写” CMD(以及ENTRYPOINTRUN)。使用JSON数组完成此操作后,您可以精确指定命令行中的“单词”,例如,-jar ${artifact}将作为包含嵌入式空间的单个参数传递。如果仅传递命令行,则Docker将为您插入一个sh -c包装器,而Shell将处理单词解析和变量插值。您永远不需要在Dockerfile中手动包含sh -c

关于spring-boot - 具有参数化的Dockerfile:失败并显示XXX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63630831/

相关文章:

java - 如何在 Spring Boot 运行时添加新的数据源

javascript - 在 Docker 中使用本地卷共享运行 Gatsby

docker - 如何在 docker 容器中禁用核心文件转储

java - 为什么我的 Web 应用程序可以在 Glassfish 上运行,但不能在 Tomcat 服务器上运行?

java - Surefire 在多模块 spring-boot 项目上

java - 保留 null 值 – Java POJO 到 org.JSONObject

spring-boot - 在 Spring-Boot 应用程序中出现错误 "Cause: AMQ119031: Unable to validate user"

java - 使用 Ajax 从 Spring Controller 加载 Modal 中的数据

python - 为什么带有 Docker 的 Django 服务器在 postgresql 中运行时没有数据库?

spring - 如何使用 Spring Boot 从 tomcat 服务器部署 html