嗨,我是 Docker 的新手,正在尝试从头开始编写一个新图像。我正在编写这个 dockerFile 来编译和运行同一目录中可用的简单 java 程序。
这是码头文件。
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
原文由 Priyanka.Patil 发布,翻译遵循 CC BY-SA 4.0 许可协议
解释
来自 Dockerfile 参考。
这就是为什么
javac
命令没有被执行并且启动容器导致no such file or directory
被发现。CMD
和ENTRYPOINT
用于执行容器(入口点级别)后应启动的任务。这适用于行
CMD java HelloWorld
,但不适用于CMD javac HelloWorld.java
这更像是一个构建步骤。这就是RUN
的用途。解决方案
将第二行更改为
RUN javac HelloWorld.java
。更新
正如 Diyoda 指出的那样,确保
FROM
图像提供 java.lang.