DockerFile to run a java program

前端 未结 8 1485
礼貌的吻别
礼貌的吻别 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:37

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

    Step 1. Create a java file 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

    Step 2. Create manifest.txt file

    Manifest-Version: 1.0
    Main-Class: HelloWorld
    

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

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

    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)

    Note: Run the command prompt as administrator for windows or prefix sudo in all commands for linux

    Step 6. Skipping (push and pull) - Optional

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

    Example:

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

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

    "Hello World"

    PS. Thanks Julia Bondarchuk

    -- Happy coding :)

提交回复
热议问题