Try Hello World using plain Java on a Docker container

Create a Java class that outputs Hello World

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.

Set up a Java executable image

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 .

Java execution on Docker!

Output


Step 4/4 : RUN java Main
 ---> Running in aab904e1114c
Hello Docker!

Recommended Posts

Try Hello World using plain Java on a Docker container
Try to build a Java development environment using Docker
Try using Redmine on Mac docker
Run React on a Docker container
Hello World on AWS Lambda + Java
Run PureScript on a Docker container
Java 10 (JDK 10) was released on March 20, 2018, so let's try Hello World.
Try building a GPU container on GCP.
Hello World on Mac VS Code Java
Java, Hello, world!
Java Hello World
Access MySQL on a Docker container from a local (host OS) Java program
Try using Firebase Cloud Functions on Android (Java)
Create a Lambda Container Image based on Java 15
Build a Kotlin app using OpenJDK's Docker container
Run JSP Hello World with Tomcat on Docker
Beginners try using android studio Part 1 (Hello World)
Send emails using Docker container on Raspberry Pi 3
I tried running Ansible on a Docker container
Display "Hello World" in the browser using Java
Try communication using gRPC on Android + Java server
Try using another Servlet container Jetty with Docker
Hello World, a cross-platform GUI app with Groovy running on the Java platform
"Hello World" in Java
Java Learning (1)-Hello World
Hello World in Java
Hello World on WebAssembly
Create a Java development environment using jenv on Mac
Run the Android emulator on Docker using Android Emulator Container Scripts
I tried using Log4j2 on a Java EE server
[Docker] Build an Apache container on EC2 using dockerfile
Try image classification using TensorFlow Lite on Android (JAVA)
Creating a lightweight Java environment that runs on Docker
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
Create a Java (Maven) project with VS Code and develop it on a Docker container
Creating a docker host on AWS using Docker Machine (personal memorandum)
Try using RocksDB in Java
Try scraping using java [Notes]
Try Docker on Windows 10 Home
Oracle Java 8 on Docker Ubuntu
java hello world, compile, run
Try launching a webAP server on the micro using Helidon
I tried running a Docker container on AWS IoT Greengrass 2.0
Java beginners read Hello World
Using Docker from Java Gradle
Make a rhombus using Java
People using docker Try using docker-compose
I tried deploying a Docker container on Lambda with Serverless Framework
Using Java on OSX 10.15 (Catalina) β
How to make a Java container
Upload a file using Java HttpURLConnection
Hello World for ImageJ Java Plugin
Run GUI application on Docker container
Hello World with Docker and C
Try using Redis with Java (jar)
Create a Java project using Eclipse
Hello World in java in eclipse now
[Java] Try to implement using generics
Try Docker on Windows Home (September 2020)
Try using IBM Java method tracing
Using Docker on Windows10 Home WSL2