DockerFile运行java程序

前端之家收集整理的这篇文章主要介绍了DockerFile运行java程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

嗨,我是Docker的新手,并尝试从头开始编写新图像.我正在编写这个dockerFile来编译和运行同一目录中可用的简单java程序.

这是dockerfile.

FROM scratch
CMD javac HelloWorld.java
CMD java HelloWorld

Docker构建成功,如下所示

[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

但是当我尝试运行时,它会抛出以下错误

[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后更新.

[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 reference.

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命令并启动容器导致没有找到这样的文件或目录.

CMD和ENTRYPOINT用于执行容器(入口点级别)后应启动的任务.

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

这适用于行CMD java HelloWorld,但不适用于CMD javac HelloWorld.java,它更像是构建步骤.这就是RUN的用途.

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

FROM scratch
RUN javac HelloWorld.java
CMD java HelloWorld

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

更新

正如Diyoda所指出的,确保FROM映像提供java.

原文链接:https://www.f2er.com/docker/436967.html

猜你在找的Docker相关文章