Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
After creating, compile with javac Main.java
and create a Main.class
file.
Next, create an image to execute java based on ubuntu.
Dockerfile
FROM ubuntu:latest
COPY Main.class Main.class
RUN apt-get update && apt-get install -y default-jre && apt-get install -y default-jdk
RUN java Main
Specify the original image (ubuntu this time) to be used in the first line.
In the second line, copy the class file for execution to the image directory.
The third line installs the JDK and makes the java
command executable.
Finally, execute the Main class on the 4th line to output Hello world.
After creating the Dockerfile, build the image with the following command, and then use the tag option to name it.
(Don't forget the last .
)
docker-build
docker build -t running-java .
Output
Step 4/4 : RUN java Main
---> Running in aab904e1114c
Hello Docker!
Recommended Posts