About Hariharan Narayanan

Hari graduated from the School of Computer and Information Sciences in the University of Hyderabad. Over his career he has been involved in many complex projects in mobile applications, enterprise applications, distributed applications, micro-services, and other platforms and frameworks. He works as a consultant and is mainly involved with projects based on Java, C++ and Big Data technologies.

You should have Docker installed already to follow the examples. Please go to the Docker home page to install the Docker engine before proceeding further, if needed. On Linux, please do ensure that your user name is added into the “docker” group while installing so that you need not invoke sudo to run the Docker client commands. You should also have Java and Gradle installed to try examples 3 and 4. Install your favorite J2SE distribution to get Java and install Gradle from gradle.org.

So let us get started!

2. Run Docker Hello World image provided by Docker

This is the simplest possible way to verify that you have Docker installed correctly. Just run the hello-world Docker image in a terminal.

$ docker run hello-world

This command will download the hello-world Docker image from the Dockerhub, if not present already, and run it. As a result, you should see the below output if it went well.

Run Docker Hello-World Image

3. Get a “Hello, world” Printed from Another Basic Docker Image

This too is simple. Docker provides a few baseimages that can be used directly to print a “Hello, World”. This can be done as shown below and it verifies that you have a successful installation of Docker.

$ docker run alpine:latest "echo" "Hello, World"

This command downloads the Alpine baseimage the first time and creates a Docker container. It then runs the container and executes the echo command. The echo command echoes the “Hello, World” string. As a result, you should see the output as below.

Use the Alpine Docker Image to Print Hello World

4. Write a Simple “Hello, World” Program in Java and Run it Within a Docker Container

Let us take it up a notch now. Let us write a simple hello world program in Java and execute it within a Docker container.

4.1. Create HelloWorld.java

First of all, let us create a simple Java program that prints “Hello, World”. Open up your favorite text editor and enter the following code:

The complete Dockerfile syntax can be found from Docker docs but here is briefly what we have done. We extend our image from the Alpine baseimage. We next added HelloWorld.class into the image with the same name. Later we installed a JRE environment using OpenJDK. Finally, we gave the command to execute when this image is run – that is to run our HelloWorld in the JVM.

4.3. Create Docker Hello World Image and Run it

Now, build an image from this Dockerfile by executing the below command.

$ docker build --tag "docker-hello-world:latest" .

As a result of this you should see the following output

Build Docker Image Called docker-hello-world

Finally, run the Docker image to see the "Hello, World" printed.

$ docker run docker-hello-world:latest

As a result of this you should see the below output

Run the Docker Image docker-hello-world

That’s it. This verifies that you can create your own custom Docker images too and run them.

5. Execute a “Hello, World” Program in Java Using Gradle and Docker

Let us take it up another notch now. Real world programs are more complex than the ones shown above. So let us create another hello world Java program and run it in a Docker container using a few best-practices of the Java ecosystem. Furthermore, let us set up a Java project using Gradle, add the Docker plugin for Gradle into it, create a Docker image and run it using Gradle.

5.1. Initialize a New Java Project and Create HelloWorld.java

First of all, Create a new folder called “docker-hello-world-example”, open a terminal and change into this folder. Next, use Gradle to initialize a new Java project.

$ gradle init --type java-library

Once done you should see the following folder structure created. You can freely delete the files Library.java and LibraryTest.java. We will not be needing them.

Folder structure for the Sample Java Project Created by Gradle

Now, create a new file src/main/java/com/javacodegeeks/examples/HelloWorld.java and enter the following in it. Save it once done.

Let us quickly go through what we did in the above Dockerfile. We derive our image from the Alpine Linux base image. Next, we installed J2SE from OpenJDK. Next, we copied the jar file generated by the build process as app.jar into the Docker image. Finally, we set the command to be run when the Docker container is run which is to execute the app.jar file.

5.3. Include Gradle-Docker Plugin

Next, make the following changes into the build.gradle file to update the Jar manifest.

Here we basically added the Gradle plugins called docker and docker-run provided by Palantir. These plugins enable us to create Docker containers and run them using Gradle. We added the URL to tell where to find the gradle-docker plugin (https://plugins.gradle.org/m2/). We also added the plugins into the classpath as build dependencies.

5.4. Configure the Docker Tasks in Gradle Build File

Next, insert the following changes at the bottom of build.gradle file to configure the details of the Docker image.

Here we configured the details of the docker image. We assigned the image name com.javacodegeeks.examples/docker-hello-world with version 1. We assigned it the tag latest and gave the path where to find the Docker file. Finally, we made it depend on the Gradle task jar so that the output of the jar task will be fed to this task.

Finally, insert the following changes at the bottom of build.gradle file to configure the details of the Docker container.

Here we configured the details of the docker container. We assigned the container name “docker-hello-world-container”. We assigned it the image from where to create the container and gave it a command to execute when running the container.

5.5. Create Docker Container and Run it

Now we are ready to build and run this. Use Gradle to build and run this program

$ ./gradlew clean build docker dockerRun

You will notice that the code was built, jar was created, Docker image and containers were created, and the container was run too. However, there is no “Hello, World” actually printed on the screen here.

Run the Gradle Tasks clean, build, docker, and dockerrun

The last line simply says that the Docker container ‘docker-hello-world-container’ is running. To see if it printed “Hello, World” successfully, we need to check the logs created by the Docker container. This can be checked by executing the following command.

$ docker logs docker-hello-world-container

Check the Docker Container Logs to Check if Hello world was Printed

There it is!

6. Summary

In this example we learned how to create Docker Hello world type containers in 4 different ways:

First, we learned how to run the hello-world image provided by Docker. This printed Hello World as soon as it was run

Next, we learned how to run the Alpine image and print a Hello World using the echo shell command

Next, we learned how to get the basic Hello World program into a Docker image and run it to print Hello World. The source code for this can be downloaded from the links given below.

Finally, we learned how to set up a structured Java project using Gradle and generate Docker images and Containers using Gradle plugins. We used this structure to create a Hello World Java project and got it to run in a container. Later, we verified the Docker logs for the container to see that Hello World was printed when the container was run. The source code for this also can be downloaded from the links given below.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.