可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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.
回答2:
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:
Another way...
- you have to use "java:8" as base image or install jdk on "ubuntu" image.
build the image
docker build -t imagename .
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
回答4:
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:
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.
回答6:
You can either use CMD or ENTRYPOINT.
Sample :
CMD ["sh", "-c", "java -jar Service.jar"]
回答7:
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 :)
回答8:
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/