DockerFile to run a java program

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

    Explanation

    From the 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.

    That is why the javac command is not executed and starting your container results in no such file or directory was found.

    CMD and ENTRYPOINT are used for the tasks that shall be started once you execute the container (entrypoint level).

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

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

    Solution

    Change the second line to 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.

    Update

    As Diyoda pointed out, make sure that the FROM image supplies java.

提交回复
热议问题