Run Java Hello World Program On Docker Container

By | November 19, 2019
Run Java Hello World Program On Docker Container
Run Java Hello World Program On Docker Container – devopsrider.com

In this tutorial I’ll tell you how to run a simple Java Hello World program on docker container with the help of Dockerfile. Before starting this tutorial you should have prior knowledge of What is Docker and how to write a simple java hello world program.

I am a Linux user so I’ll use my terminal to type commands. If you are using windows then you can use git bash.

Switch your system into root account.

devopsrider@del1-lhp-n02552:~ sudo su

First of all make sure that you have installed docker on your machine. You can follow my tutorial where I explained What is Docker? | How Docker Works? | Learn Docker Basic Commands Also make sure you have jdk in your machine to convert .java file into .class file.

After docker installation check its version. It shows you have installed docker successfully.

root@del1-lhp-n02552:~# docker --version
Docker version 18.09.2, build 6247962

Create Java Hello World Program

Now create a folder with any name. I created a folder devopsrider.

root@del1-lhp-n02552:~# mkdir devopsrider
root@del1-lhp-n02552:~# cd devopsrider

Now I’ll create a Hello World Java program with Test.java file name

root@del1-lhp-n02552:~/devopsrider# vi Test.java

A file will open where you have to paste the following code. You can write your own code here but for simplicity I am writing a simple Hello World Program.

public class Test{
        public static void main(String args[]){
                System.out.println("Hello World");

        }
}

Compile this java file with following command –

root@del1-lhp-n02552:~# javac Test.java

Now if you will type ‘ls’ command then you will see two file here.

root@del1-lhp-n02552:~/devopsrider# ls
Test.class  Test.java

Create .jar File

Now you have to create a manifest file –

root@del1-lhp-n02552:~/devopsrider# vi manifest.txt

In manifest file paste following code –

Manifest-Version: 1.2
Main-Class: Test

Now convert .class file into .jar file. Enter following command –

root@del1-lhp-n02552:~/devopsrider# jar cvfm Test.jar manifest.txt Test.class
added manifest
adding: Test.class(in = 413) (out= 287)(deflated 30%)

Enter ‘ls’ command to check all files in directory –

root@del1-lhp-n02552:~/devopsrider# ls
manifest.txt  Test.class  Test.jar  Test.java

Create Dockerfile

Now create and open a docker file with name Dockerfile

root@del1-lhp-n02552:~# vi Dockerfile

And paste following code in it.

FROM openjdk:8
ADD Test.jar Test.jar
ENTRYPOINT ["java", "-jar", "Test.jar"]

We’ll execute this dockerfile and will create a docker image with hello-world-image name. When this docker image will be running on container it will tell what have to be happened on container.

In Dockerfile the first line will install openjdk as a base image in your docker image. Second line will add Test.jar file from your local machine to you docker image. And third line will run this Test.jar file on container.

Now execute this Dockerfile and it will generate an image. I am giving a name hello-world-image to this image. Make sure that the image name is in small letters.

Create hello-world-image

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

Run hello-world-image on Container

Now your image is ready to run on container. Check your image by following docker command –

root@del1-lhp-n02552:~/devopsrider# docker images

REPOSITORY         TAG      IMAGE ID      CREATED              SIZE
hello-world-image  latest  24b1a0acd314    About a minute ago   488MB
openjdk            8       57c2c2d2643d    4 weeks ago          488MB

Now it’s time to run your hello world java program on docker container.

root@del1-lhp-n02552:~/devopsrider# docker run hello-world-image

Hello World

Now if you will check this container by following command –

root@del1-lhp-n02552:~/devopsrider# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

You will see no hello-world-image container is running because your container ran and printed Hello World and stopped(exited) as it has completed given task. If I would have tried to run a spring boot web application on a particular port then it would run continuously and you could see that running container here.

You can see your exited container with following command –

root@del1-lhp-n02552:~/devopsrider# docker ps -a

CONTAINER ID    IMAGE                COMMAND              CREATED         STATUS                   PORTS               NAMES
b28902a53ce6    hello-world-image   "java -jar Test.jar"  44 seconds ago      Exited (0) 42 seconds ago                          mystifying_visvesvaraya

I hope you liked this tutorial. Please let me know in comment box if you have any doubt or query.

Leave a Reply

Your email address will not be published. Required fields are marked *