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