我在一个简单的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的ENTRYPOINT
和CMD
行get combined into a single command line。在您的Dockerfile中,这被解释为sh -c java '-jar ${artifact}'
但是sh -c
选项实际上仅接收下一个单词并将其解释为要运行的命令;所以真的被处理为sh -c 'java' # '-jar ${artifact}'
忽略-jar
选项。有two,ways来“拼写”
CMD
(以及ENTRYPOINT
和RUN
)。使用JSON数组完成此操作后,您可以精确指定命令行中的“单词”,例如,-jar ${artifact}
将作为包含嵌入式空间的单个参数传递。如果仅传递命令行,则Docker将为您插入一个sh -c
包装器,而Shell将处理单词解析和变量插值。您永远不需要在Dockerfile中手动包含sh -c
。
关于spring-boot - 具有参数化的Dockerfile:失败并显示XXX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63630831/