Docker Tutorial with Hello World Example

Docker is a developer tool to package applications along with their runtime environment, so anybody can deploy and run them in any other machine without facing runtime environment conflicts. It is very similar to virtual machine concept (virtualization), where you can get a VM image and run it on any supporting hardware. All internal programs in VM will function as they were packaged originally.

Difference between a VM and a docker image is that docker image does not package whole virtual operating system. It uses the OS resources just like other process in developer’s machine, only application and it’s runtime specific dependencies are packaged (Containerization).

Virtualization V/S Containerization

Docker allows users to publish docker images and consume those published by others in repositories like Docker Hub.

In this tutorial, learn to install Docker container in windows, to create docker image and to deploy Docker image (which as one simple spring boot based microservice) in developer machine.

Table of Contents

Docker Installation
Create Docker Image
Deploy and Run Docker Image

Docker Installation

To install docker on Windows 7 machine, follow the bellow steps:

Choose the appropriate Docker installer for your System

Before starting with the installation process we need to understand the exact Docker version that is suitable for the Windows that you are using. Docker has provided two versions of Windows distribution as bellow

We will follow the Docker tool box installation steps for this article.

Download Docker installer

We need to first download the Docker toolbox distribution from and we will follow the installation steps in local Workstation.

Enable Hardware Virtualization Technology

In order to Docker toolbox works properly we need to make sure your Windows system supports Hardware Virtualization Technology and that virtualization is enabled. Docker has provided detailed step on this here: If we don’t have this enabled then we need to go to BIOS option and enable Hardware Virtualization. The BIOS is bit different for different models of Computer, so please follow official guideline for enabling that.

Run Docker installer

Once we have the Installer downloaded and we have enabled the Hardware Virtualization, we can start the installer. It’s just like a simple another windows based installation process guided by a installation wizard.

Verify your installation

To verify docker installation, open Docker Quickstart Terminal shortcut from either Desktop or Start menu. Verify that Docker prompt is coming and then need to test few basic commands. Docker prompt and sample docker command will look like below.

Docker installation verification

Note Down the Docker IP

We need to now note down the Docker IP assigned to this Container. We will access this IP to access the Applications installed inside Docker. To know the IP from the command prompt use command docker-machine ip. Here is the sample output of the command. Please note that this IP will be different for different M/Cs.

docker-machine ip output

Create docker Image

We will first create a spring boot based REST API, add docker specific configuration and then we will create docker image.

Create Spring REST Project

Develop one simple hello world Microservice for testing. We have used spring boot and Maven and Eclipse as IDE. Add a REST endpoints so that once this application is deployed in to Docker, we can test this by accessing the rest endpoint.

package com.example.howtodoinjava.hellodocker;

import java.util.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloDockerApplication {

	public static void main(String[] args) {, args);

class HelloDockerRestController {
	public String helloDocker(@PathVariable(value = "name") String name) {
		String response = "Hello " + name + " Response received on : " + new Date();
		return response;


Update resources/ with server port information.

server.port = 9080

Now test this microservice by running the project as spring boot application.

Add Docker Configurations

Now create a file named Dockerfile in the root directory and add the below lines as Docker configurations.

FROM openjdk:8-jdk-alpine
ADD target/hello-docker-0.0.1-SNAPSHOT.jar hello-docker-app.jar
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /hello-docker-app.jar" ]

This is used by Docker while creating the image. It is basically declaring the Java runtime information and target distributions. For more details, follow docker builder reference.

Add Maven Docker Plugins

Add two maven plugins in the pom.xml file so that we can use the Docker related maven commands while creating the instance. Those plugins are dockerfile-maven-plugin and maven-dependency-plugin.

We have used the minimal configurations required to build the project.


Create Docker Image

Now use maven command mvn clean install dockerfile:build to create docker image.

Docker Image build from Docker terminal
Please make sure your local application is not running while you are building the image, in that case you might get maven build failure, as in clean step it will not be able to delete the target folder as the jar is being used by java process.

Here is the last few lines of maven output log where it is building the image.

[INFO] Image will be built as hello-howtodoinjava/hello-docker:latest
[INFO] Step 1/5 : FROM openjdk:8-jdk-alpine
[INFO] Pulling from library/openjdk
[INFO] Digest: sha256:2b1f15e04904dd44a2667a07e34c628ac4b239f92f413b587538f801a0a57c88
[INFO] Status: Image is up to date for openjdk:8-jdk-alpine
[INFO]  ---> 478bf389b75b
[INFO] Step 2/5 : VOLUME /tmp
[INFO]  ---> Using cache
[INFO]  ---> f4f6473b3c25
[INFO] Step 3/5 : ADD target/hello-docker-0.0.1-SNAPSHOT.jar hello-docker-app.jar
[INFO]  ---> ce7491518508
[INFO] Removing intermediate container c74867501651
[INFO] Step 4/5 : ENV JAVA_OPTS ""
[INFO]  ---> Running in f7cd27710bf3
[INFO]  ---> 086226135205
[INFO] Removing intermediate container f7cd27710bf3
[INFO] Step 5/5 : ENTRYPOINT sh -c java $JAVA_OPTS -jar /hello-docker-app.jar
[INFO]  ---> Running in 9ef14a442715
[INFO]  ---> bf14919a32e2
[INFO] Removing intermediate container 9ef14a442715
[INFO] Successfully built bf14919a32e2
[INFO] Successfully tagged hello-howtodoinjava/hello-docker:latest
[INFO] Detected build of image with id bf14919a32e2
[INFO] Building jar: F:\Study\Technical Writings\docker\hello-docker\target\hello-docker-0.0.1-SNAPSHOT-docker-info.jar
[INFO] Successfully built hello-howtodoinjava/hello-docker:latest
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

Deploy and Run Docker Image

So we have created the Docker Image (i.e. hello-docker-0.0.1-SNAPSHOT-docker-info.jar). We also have a installed docker container running in our local machine.

Now, to run the docker image inside installed docker container, we will use below command.

docker run -p 8080:9080 -t hello-howtodoinjava/hello-docker  --name hello-docker-image

Here the option -p 8080:9080 is important. It says that expose port 8080 for internal port 9080. Remember our application is running in port 9080 inside docker image and we will access that in port 8080 from outside Docker container.

Now access the application with URL Notice that the browser output is same as output of standalone REST API on localhost.

Docker Localhost Output
Docker Localhost Output

Stop Docker Container

We can list down all docker containers by command docker ps in the terminal and we can use command docker stop <name>

Stop Docker Container


We learned to install Docker in Windows OS. We also learned to create a spring boot project with one REST endpoint and build Docker image for it. Then we learned to run the docker image inside docker container and tested REST endpoint inside docker image.

Docker is very cool tool to solve very old developer problem that “it works in my local machine”. Now if something works in your machine, you can surely run that on other machine as well.

Happy Learning!!!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

14 thoughts on “Docker Tutorial with Hello World Example”

  1. Great Man. Have one question if i modify java file and wants to run updated code so should i create docker image again to see the changes or any other way to update an existing image, Because if i create again docker image it will download all dependencies again.

  2. Thanks for the article nice info. about Docker.

    if you phase any errors in windows machine use below commands.

    to build:- docker build -t test_dockereg_images .
    to run:– docker run -p 8080:9200 -t test_dockereg_images –name hello-docker-image

    You check in the URL you will get the response.


  3. Nice Article about docker.

    Do the changes what ever you see in the above if you face any issue in windows machine like compile time errrors

    if any one facing while building the docker file use below command for building and to run

    for building use this cmd —-? docker build -t test_dockereg_images

    to run use this cmd —> docker run -p 8080:9200 -t test_dockereg_images –name hello-docker-image

    Check the URL u will see the changes.


  4. The Build has failed with “ org.apache.http.conn.HttpHostConnectException: Connect to localhost:2375 [localhost/, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect”
    Can anybody please suggest what can be done here.

    • Hello Wilson.,

      Did you try to execute the above tutorial in windows? If yes , did you face the following error – Could not build image: java.util.concurrent.ExecutionException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:2375 [localhost/, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect -> [Help 1]

      Thanks & Regards

      • I ran into this problem aswell. I was able to fix by changing the Docker Desktop Settings
        – Click on the Docker Icon -> Settings -> General -> Check the box “Expose daemon on tcp://localhost:2375 without TLS”

        • i am using docker on windows 7, in my system, there is no docker desktop, i have only docker tool box, inside docker tool box we have docker-machine cli based tool, how to enable export daemon option in my set up

  5. for me it worked very nicely. thank you so much. Great. Great. I feel that I have achieved something. Very simple demo. We have learnt very easily such a challenging task.

  6. I tried to run this below command :
    docker run -p 8080:9080 -t hello-howtodoinjava/hello-docker –name hello-docker-image

    $ docker run -p 8080:9080 -t hello-howtodoinjava/hello-docker –name hello-docker
    –name: 1: –name: Syntax error: Unterminated quoted string
    Could you please tell me what is the issue with this command?

  7. I am current getting below error while building in the windows Docker toolbox. Your help is appreciated.

    [ERROR] Get dial tcp: lookup registry-1.docker
    .io on no such host
    [INFO] ————————————————————————
    [INFO] ————————————————————————
    [INFO] Total time: 26.691 s
    [INFO] Finished at: 2017-11-20T15:34:08+11:00
    [INFO] Final Memory: 51M/636M
    [INFO] ————————————————————————
    [ERROR] Failed to execute goal com.spotify:dockerfile-maven-plugin:1.3.4:build (
    default-cli) on project hello-docker: Could not build image: Get https://registr dial tcp: lookup on no such
    host -> [Help 1]

Comments are closed.


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.