运行java程序的DockerFile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35291076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:30:30  来源:igfitidea点击:

DockerFile to run a java program

javadocker

提问by Priyanka.Patil

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.

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

Here is the dockerfile.

这是 dockerfile。

FROM scratch
CMD javac HelloWorld.java
CMD java HelloWorld

Docker build is successful as shown below

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

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

Please help to resolve the same.

请帮助解决相同的问题。



Update after chaning second line to RUN.

将第二行更改为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

采纳答案by michaelbahr

Explanation

解释

From the Dockerfile reference.

来自Dockerfile 参考

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.

一个 Dockerfile 中只能有一个 CMD 指令。如果你列出了多个 CMD,那么只有最后一个 CMD 会生效。

That is why the javaccommand is not executed and starting your container results in no such file or directorywas found.

这就是为什么javac不执行命令并找到启动容器结果的no such file or directory原因。

CMDand ENTRYPOINTare used for the tasks that shall be started once you execute the container (entrypoint level).

CMDENTRYPOINT用于执行容器后应启动的任务(入口点级别)。

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

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

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

这适用于 line CMD java HelloWorld,但不适用于CMD javac HelloWorld.java哪个更像是构建步骤。那RUN是为了什么。

Solution

解决方案

Change the second line to RUN javac HelloWorld.java.

将第二行更改为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.

生成的提交图像 [来自第二行] 将用于 Dockerfile 中的下一步。

Update

更新

As Diyodapointed out, make sure that the FROMimage supplies java.

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

回答by Nilo_DS

Another way to run it could be with a shell file.

另一种运行它的方法可能是使用 shell 文件。

CMD ["/bin/bash", "-ex", "run.sh"]

and in your run.sh file you can run the javac and java commands.

在 run.sh 文件中,您可以运行 javac 和 java 命令。

回答by Shashank G

Another way...

其它的办法...

  1. you have to use "java:8" as base image or install jdk on "ubuntu" image.
  2. build the image

    docker build -t imagename .
    
  3. run it(mounting Helloworld.java to container)

    docker run -it -v ~/system-path:/javafolder imagename
    
  1. 您必须使用“java:8”作为基础镜像或在“ubuntu”镜像上安装 jdk。
  2. 建立形象

    docker build -t imagename .
    
  3. 运行它(将 Helloworld.java 挂载到容器)

    docker run -it -v ~/system-path:/javafolder imagename
    

type these commands to execute inside container-

键入这些命令以在容器内执行 -

cd javafolder

javac HelloWorld.java

java HelloWorld

回答by Amey Jadiye

You can save yourself by writing DockerFile as well, just have java image in your local image repo, compile and run your java program by passing your program to it, its very easy.

您也可以通过编写 DockerFile 来拯救自己,只需在您的本地图像存储库中放置 Java 图像,通过将您的程序传递给它来编译和运行您的 Java 程序,这非常简单。

$ docker run java:alpine java -version

$ docker run --rm -v $PWD:/app -w /app java:alpine javac Main.java

$ docker run --rm -v $PWD:/app -w /app java:alpine java Main

回答by sandeep kumar sahoo

FROM java:8

从 java:8

WORKDIR /

ADD HelloWorld.java HelloWorld.java

CMD javac HelloWorld.java

ADD HelloWorld.class HelloWorld.class

CMD java Test

Update the above details in your Dockerfile the rebuild the docker image and run.

更新 Dockerfile 中的上述详细信息,重建 docker 映像并运行。

回答by rex roy

You can either use CMD or ENTRYPOINT.

您可以使用 CMD 或 ENTRYPOINT。

Sample :

样本 :

CMD ["sh", "-c", "java -jar Service.jar"]

CMD ["sh", "-c", "java -jar Service.jar"]

回答by Rajat Gupta

Here I am writing all commands and code that need to execute in order to run a hello world program on docker container without any build tool like gradle or maven.

在这里,我正在编写需要执行的所有命令和代码,以便在没有任何构建工具(如 gradle 或 maven)的情况下在 docker 容器上运行 hello world 程序。

devopsrider@del1-lhp-n02552:~ sudo su
root@del1-lhp-n02552:~# mkdir devopsrider
root@del1-lhp-n02552:~# cd devopsrider
root@del1-lhp-n02552:~/devopsrider# vi Test.java

public class Test{
        public static void main(String args[]){
                System.out.println("Hello World");

        }
}

root@del1-lhp-n02552:~# javac Test.java
root@del1-lhp-n02552:~/devopsrider# ls
Test.class  Test.java

root@del1-lhp-n02552:~/devopsrider# vi manifest.txt
Manifest-Version: 1.2
Main-Class: Test

root@del1-lhp-n02552:~/devopsrider# jar cvfm Test.jar manifest.txt Test.class
added manifest
adding: Test.class(in = 413) (out= 287)(deflated 30%)

root@del1-lhp-n02552:~/devopsrider# ls
manifest.txt  Test.class  Test.jar  Test.java

root@del1-lhp-n02552:~# vi Dockerfile
FROM openjdk:8
ADD Test.jar Test.jar
ENTRYPOINT ["java", "-jar", "Test.jar"]

root@del1-lhp-n02552:~/devopsrider# docker build -f Dockerfile -t hello-world-image .

Sending build context to Docker daemon  6.656kB
Step 1/3 : FROM openjdk:8
8: Pulling from library/openjdk
Digest: sha256:c168e211f317cc5db38b19fe62641316dbc1e60e5b53ad45ee440ba8152c20b9
Status: Downloaded newer image for openjdk:8
 ---> 57c2c2d2643d
Step 2/3 : ADD Test.jar Test.jar
 ---> b7d512e51b60
Step 3/3 : ENTRYPOINT ["java", "-jar", "Test.jar"]
 ---> Running in 07c871318e8a
Removing intermediate container 07c871318e8a
 ---> 24b1a0acd314
Successfully built 24b1a0acd314
Successfully tagged hello-world-image:latest

root@del1-lhp-n02552:~/devopsrider# docker run hello-world-image
Hello World

For complete tutorial to run java Hello World program on docker container visit http://www.devopsrider.com/2019/11/19/hello-world-java-program-on-docker-container/

有关在 docker 容器上运行 java Hello World 程序的完整教程,请访问http://www.devopsrider.com/2019/11/19/hello-world-java-program-on-docker-container/

回答by Parthasarathy S

Try to follow this, i have mentioned all the steps to be followed below.

尝试遵循这一点,我在下面提到了所有要遵循的步骤。

Step 1. Create a java file HelloWorld.java

步骤 1. 创建一个 java 文件HelloWorld.java

public class HelloWorld {

  public static void main(String[] args){

    System.out.println("Hello World! ");

  }

Step 1.1 Generate class file Ex: javac HelloWorld.java

步骤 1.1 生成类文件例如: javac HelloWorld.java

Step 2. Create manifest.txtfile

步骤 2. 创建manifest.txt文件

Manifest-Version: 1.0
Main-Class: HelloWorld

Step 3. Create jar file Ex: jar cfm HelloWorld.jar manifest.txt HelloWorld.class

步骤 3. 创建 jar 文件例如: jar cfm HelloWorld.jar manifest.txt HelloWorld.class

Step 4. Create a file with name Dockerfile(no extension)

步骤 4. 创建一个名为Dockerfile的文件(无扩展名)

FROM java:7

WORKDIR /

ADD HelloWorld.jar HelloWorld.jar

EXPOSE 8080

CMD java -jar HelloWorld.jar

Step 5. Come out of the current directory For exampe: From C:/custom-docker/java-docker to C:/custom-docker and run this cmd docker build -t java-docker(use the folder name, here java-docker)

Step 5. 退出当前目录 例如:从 C:/custom-docker/java-docker 到 C:/custom-docker 并运行这个 cmd docker build -t java-docker(使用文件夹名称,这里是 java-docker)

Note: Run the command prompt as administratorfor windows or prefix sudoin all commands for linux

注意:以管理员身份运行Windows的命令提示符或在 linux 的所有命令中添加前缀sudo

Step 6. Skipping (push and pull) - Optional

步骤 6. 跳过(推和拉) - 可选

Step 7. Run this cmd docker imagesand find the latest Image ID

步骤 7. 运行此 cmddocker images并找到最新的 Image ID

Example:

例子:

REPOSITORY | TAG | IMAGE ID | CREATED SIZE < none > | < none > | 58df7fdecdeb| 3 minutes ago

存储库 | 标签 | 图像 ID | 创建大小 < 无 > | <无> | 58df7fdecdeb| 3 分钟前

Step 8. Finally run this cmd docker run 58df7fdecdeb, and you could see output as

步骤 8. 最后运行这个 cmd docker run 58df7fdecdeb,你可以看到输出为

"Hello World"

“你好,世界”

PS. Thanks Julia Bondarchuk

附注。感谢朱莉娅·邦达尔丘克

-- Happy coding :)

-- 快乐编码 :)