Docker Hello World Example
In this example we will explore different ways of running basic “Hello, World” containers in Docker.
1. Introduction
Creating a Docker Hello World container and getting it to work verifies that you have the Docker infrastructure set up properly in your development system. We will go about this in 4 different ways:
- Run Docker hello world image provided by Docker
- Get a “Hello, world” printed from another basic Docker image
- Write a Simple “Hello, World” Program in Java and Run it Within a Docker Container
- Execute a “Hello, World” Program in Java Using Gradle and Docker
Table Of Contents
- 1. Introduction
- 2. Run Docker Hello World image provided by Docker
- 3. Get a “Hello, world” Printed from Another Basic Docker Image
- 4. Write a Simple “Hello, World” Program in Java and Run it Within a Docker Container
- 5. Execute a “Hello, World” Program in Java Using Gradle and Docker
- 6. Summary
- 7. Download
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.
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.

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:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
This is a standard HelloWorld program in Java very closely resembling the one provided in the Official Java tutorial. Save the file as HelloWorld.java. Now, compile this file using the Java compiler.
$ javac HelloWorld.java
This should create the class file Helloworld.class. Normally, we would use the java command to execute HelloWorld as below

But we want to run this from within a Docker container. To accomplish that we need to create a Docker image. Let us do that now.
4.2. Create a Dockerfile
To create a new Docker image we need to create a Dockerfile. A Dockerfile defines a docker image. Let us create this now. Create a new file named Dockerfile and enter the following in it and save it.
FROM alpine:latest ADD HelloWorld.class HelloWorld.class RUN apk --update add openjdk8-jre ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "HelloWorld"]
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

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

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.

Now, create a new file src/main/java/com/javacodegeeks/examples/HelloWorld.java and enter the following in it. Save it once done.
package com.javacodegeeks.examples;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}





To resolve “java.lang.ClassNotFoundException: org.gradle.api.internal.component.Usage”, simply change the version of the gradle-docker plugin from 0.9.1 to 0.13.0.
To resolve “Cannot get the value of write-only property ‘dockerfile’ for object of type com.palantir.gradle.docker.DockerExtension.”, simply change:
dockerfile ‘src/main/docker/Dockerfile’
to
dockerfile project.file(‘src/main/docker/Dockerfile’)
There is a path mismatch in this example, step 5.2 uses: “src/main/resources/Dockerfile”, but the build.gradle file contains, “dockerfile ‘src/main/docker/Dockerfile'” (See 5.4)
Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!
https://www.besanttechnologies.com/training-courses/other-training-courses/devops-training-institute-in-chennai
Excellent post, truly well post. It will be used for improve our self. Thank you for sharing this content…!
I am a beginner at java programming. Your blog is very useful for me.
Wow!! Really a nice Article. Thank you so much for your efforts. Definitely, it will be helpful for others. I would like to follow your blog. Share more like this. Thanks Again.
iot training in Chennai | Best iot Training Institute in Chennai
how to use volumes in docker in java program so that changes made in java program reflected during running of container without building the dockerfile again and again?
Really nice article…I think it is informative…Keep Sharing…
Testing & Training on Selenium
GREAT POST
Such a Great Article!!
Interesting Things.
Such A nice post… thanks For Sharing !!Great information for new guy like me
Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles.
To resolve “Cannot get the value of write-only property ‘dockerfile’ for object of type com.palantir.gradle.docker.DockerExtension.”, simply change:
dockerfile ‘src/main/docker/Dockerfile’
to
dockerfile project.file(‘src/main/docker/Dockerfile’)
Thank You for an amazing article, it was really helpful for me and loved to read this.
Thanks for sharing an informative blog keep rocking bring more details. I like the helpful info you provide in your articles.
STAD Solution is an IT Training and Development company. We provide Best Software testing Training in Ahmedabad.
Thank you for sharing this website.
Your new valuable key points imply much to a person like me and extremely more to my office workers. With thanks; from every one of us.
I am very new to this coding. Your blog is very helpful for me. Thanks for sharing.
Excellent information. Really love it.
“I’ve been using classroom management software for a few years now, and it has completely transformed the way I teach. It’s so much easier to organize my courses and assignments, and I can communicate with my students and parents more effectively. Highly recommend!”
Thanks for this useful article. Keep sharing & blogging. If you’re looking to study (or) Pursue a career abroad, Visit dynamic study abroad!
Excellent post.
This is an excellent post – thank you for sharing it! I always relish the opportunity to read such superb content filled with valuable information. The ideas presented here are truly best and exceptionally captivating, making the post thoroughly enjoyable. Please continue with your fantastic work.
You’re very welcome! I’m pleased to hear that the post was helpful and provided you with the information you were looking for. If you ever have more questions or need assistance in the future, don’t hesitate to ask. Thank you for your kind words, and I’m here to help with any information or guidance you may need!
https://www.slainstitute.com/devops-training-in-chennai
https://www.slainstitute.com/rpa-training-in-chennai
Excellent blog.
Excellent blog.