DockerFile运行Java程序 [英] DockerFile to run a java program

查看:481
本文介绍了DockerFile运行Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手,正在尝试从头开始编写新映像。我正在编写此dockerFile来编译并运行同一目录中可用的简单Java程序。

Hi I'm new to Docker and trying out to write a new image from the scratch. I am writing this dockerFile to compile and run a simple java program available in the same directory.

这里是dockerfile。

Here is the dockerfile.

FROM scratch
CMD javac HelloWorld.java
CMD java HelloWorld

Docker构建成功,如下所示

Docker build is successful as shown below

[root@hadoop01 myjavadir]# docker build -t runhelloworld .
Sending build context to Docker daemon 3.072 kB
Sending build context to Docker daemon
Step 0 : FROM scratch
 --->
Step 1 : CMD javac HelloWorld.java
 ---> Running in 7298ad7e902f
 ---> f5278ae25f0c
Removing intermediate container 7298ad7e902f
Step 2 : CMD java HelloWorld
 ---> Running in 0fa2151dc7b0
 ---> 25453e89b3f0
Removing intermediate container 0fa2151dc7b0
Successfully built 25453e89b3f0

但是当我尝试运行,它会引发以下错误:

But when i try to run, it throws the following error:

[root@hadoop01 myjavadir]# docker run runhelloworld
exec: "/bin/sh": stat /bin/sh: no such file or directory
Error response from daemon: Cannot start container 676717677d3f1bf3b0b000d68b60c32826939b8c6ec1b5f2e9876969c60e22a4: [8] System error: exec: "/bin/sh": stat /bin/sh: no such file or directory
[root@hadoop01 myjavadir]#  exec: "/bin/sh": stat /bin/sh: no such file or directory
bash: exec:: command not found

请帮助解决该问题。

将第二行更改为 RUN 后更新。

Update after chaning second line to RUN.

[root@hadoop01 myjavadir]# docker build -t runhelloworld . 
Sending build context to Docker daemon 3.584 kB 
Sending build context to Docker daemon 
Step 0 : FROM scratch 
---> 
Step 1 : RUN javac HelloWorld.java 
---> Running in fdef2d65ac58 
exec: "/bin/sh": stat /bin/sh: no such file or directory [8] 
System error: exec: "/bin/sh": stat /bin/sh: no such file or directory


推荐答案

说明



来自 Dockerfile参考


在Dockerfile中只能有一条CMD指令。如果您列出
个以上的CMD,则只有最后一个CMD才会生效。

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

这就是为什么 javac 命令未执行,启动容器导致找不到此类文件或目录

That is why the javac command is not executed and starting your container results in no such file or directory was found.

CMD ENTRYPOINT 用于执行在执行容器后应开始的任务(入口点级别)。

CMD and ENTRYPOINT are used for the tasks that shall be started once you execute the container (entrypoint level).


CMD的主要目的是为执行中的容器提供默认值。

The main purpose of a CMD is to provide defaults for an executing container.

适用于 CMD java HelloWorld 行,但不适用于 CMD javac HelloWorld.java 这更多的是构建步骤。这就是 RUN 的目的。

That applies to the line CMD java HelloWorld, but not to CMD javac HelloWorld.java which is more of a build step. That is what RUN is for.

将第二行更改为 RUN javac HelloWorld.java

FROM scratch
RUN javac HelloWorld.java
CMD java HelloWorld




[从第二行开始]生成的提交映像将用于Dockerfile中的下一步

The resulting committed image [from line two] will be used for the next step in the Dockerfile.

更新

Diyoda 指出,请确保 FROM 映像提供Java。

As Diyoda pointed out, make sure that the FROM image supplies java.

这篇关于DockerFile运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆