简体   繁体   English

Docker CMD + ENTRYPOINT与命令行行为不匹配

[英]Docker CMD + ENTRYPOINT not matching command line behavior

I am trying to build a vertx docker environment based on a distroless java image. 我正在尝试基于一个发行版的Java映像构建一个vertx docker环境。 When I run the docker container, it won't find the class I am trying to execute. 当我运行docker容器时,找不到我要执行的类。

However, when I run the command in the container it works just fine. 但是,当我在容器中运行命令时,它就可以正常工作。

Here is my Dockerfile 这是我的Dockerfile

FROM gcr.io/distroless/java:debug
ADD vert.x-3.7.0.tar.gz /usr/

ENTRYPOINT ["java","-cp", "'/usr/vertx/lib/*'", "io.vertx.core.Launcher", "version"]
CMD ["-start"]

When I run the container, I get the following error: 运行容器时,出现以下错误:

Error: Could not find or load main class io.vertx.core.Launcher

So I ran the image with the following command: 因此,我使用以下命令运行图像:

docker run --entrypoint=sh -ti <image_name>

which gives a busybox shell. 这提供了一个busybox外壳。 I then entered the following on the command line: 然后,我在命令行上输入了以下内容:

java -cp '/usr/vertx/lib/*' io.vertx.core.Launcher version

And it worked fine, giving me the following output 它工作正常,给我以下输出

Apr 07, 2019 7:20:18 PM io.vertx.core.impl.launcher.commands.VersionCommand
INFO: 3.7.0

I expected the same behavior from the ENTRYPOINT + CMD combination. 我期望ENTRYPOINT + CMD组合具有相同的行为。 I just can't seem to figure out why that combo isn't working. 我只是似乎无法弄清楚为什么该组合无法正常工作。

You have an error in your Dockerfile . 您的Dockerfile有一个错误。 You have: 你有:

ENTRYPOINT ["java","-cp", "'/usr/vertx/lib/*'", "io.vertx.core.Launcher", "version"]

There are too many quotes in the third parameter. 第三个参数中的引号过多。 You have: 你有:

"'/usr/vertx/lib/*'"

You want: 你要:

"/usr/vertx/lib/*"

In your current Dockerfile , the argument to the -cp option is the literal value '/usr/vertx/lib/*' , including the single quotes. 在您当前的Dockerfile-cp选项的参数是文字值'/usr/vertx/lib/*' ,包括单引号。 This of course does not match any filesystem path. 当然,这与任何文件系统路径都不匹配。

Final solution was to use the following Dockerfile: 最终解决方案是使用以下Dockerfile:

FROM gcr.io/distroless/java
ADD vert.x-3.7.0.tar.gz /usr/

ENTRYPOINT ["java","-cp", "/usr/vertx/lib/*", "io.vertx.core.Launcher", "version"]

I removed the :debug only because the busybox shell was only for debugging. 我删除:debug只是因为busybox shell仅用于调试。 It did not affect the solution. 它没有影响解决方案。

Final size was 222MB, down from about 474MB! 最终大小为222MB,低于大约474MB! I'll have to do some testing to see how fully functional the image is, but as of right now I am hopeful that this will function as a slimmed down vertx image. 我必须进行一些测试,以查看图像的功能是否完整,但是到目前为止,我希望它可以用作缩小的vertx图像。

Distroless images work a bit differently. Distroless图片的工作方式略有不同。 They expect your CMD to have a form of ["yourfile.jar", "--param1=val1", ...]. 他们希望您的CMD具有[“ yourfile.jar”,“ --param1 = val1”,...]的形式。 Note there is no "java" 注意没有“ java”

You can find this information in the buildfile of distroless java image: https://github.com/GoogleContainerTools/distroless/blob/master/java/BUILD#L28 您可以在非发行版Java映像的构建文件中找到此信息: https : //github.com/GoogleContainerTools/distroless/blob/master/java/BUILD#L28

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

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