DockerFile to run a java program

2020-07-03 07:51发布

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.

Here is the dockerfile.

FROM scratch
CMD javac HelloWorld.java
CMD java HelloWorld

Docker build is successful as shown below

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

[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

标签: java docker
8条回答
走好不送
2楼-- · 2020-07-03 07:55

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.

$ 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
查看更多
做自己的国王
3楼-- · 2020-07-03 07:56

You can either use CMD or ENTRYPOINT.

Sample :

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

查看更多
放荡不羁爱自由
4楼-- · 2020-07-03 07:57

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

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

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

查看更多
别忘想泡老子
5楼-- · 2020-07-03 08:00

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2020-07-03 08:01

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

查看更多
劫难
7楼-- · 2020-07-03 08:09

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
    

type these commands to execute inside container-

cd javafolder

javac HelloWorld.java

java HelloWorld
查看更多
登录 后发表回答