DockerFile to run a java program

前端 未结 8 1506
礼貌的吻别
礼貌的吻别 2021-02-19 16:10

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

8条回答
  •  耶瑟儿~
    2021-02-19 16:29

    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.

    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/

提交回复
热议问题