A step by step guide on how to create and build a docker image for a java application

Let me share my experience on how to create and build a docker based java application step by step. Since you’re reading this tutorial, it’s safe to assume that you already know about docker, containerization and java. Still, I will like to share a little bit about docker and containerization.

What do we mean by containerization ?

Containerization is a process of deploying and running distributed applications i.e. a group of processes in isolation without using an entire virtual machine for each distributed application and is allowed to run on a single host and access the same OS kernel.In short, containerization is a smart option over virtualization.

What is docker?

Docker is an open source containerization platform that builds, deploys and manages containerized application.In short, docker is an orchestration tool.

Now lets get started with building a docker image for java application.

For simplicity let us create a simple java console application.Below is my code snippet

Next open this code base location from git bash. Below is my screen where I have the code base

Next open vi editor to create a DockerFile, I hope you know the basic commands of vi editor.

Below are commands that I used to create the DockerFile.

vi DockerFile command will create a new empty DockerFile.

Next press enter, you can see in vi editor a new empty file.Below is my screen.

Once you are inside the editor you need to press Esc button to edit the DockerFile. In order to add the code to the DockerFile press Shift+i , this will allow you to edit the DockerFile.

Next we will update the DockerFile with set of commands. Below is my screen.

Once this is done we can save this file by using this command, press Esc and than enter wq. Below is my screen

Now, let me explain a little bit about what this docker file is doing.

  1. FROM command is the starting point of any DockerFile and tells us where the base image.For my case I used alpine distribution which is significantly smaller than an alternative flavor of Linux.
  2. WORKDIR command is used to set a working directory with directory name as my-hello-world directory. Note this step is completely optional.
  3. COPY command as the name suggest copy’s MyHelloWorld.java from the code base to this working directory.
  4. RUN command is used to install the JDK as alpine doesn't come with a JDK and also to compile the java file.
  5. ENV command is used to set the environmental variables.
  6. ENTRYPOINT or CMD command is executed when we start a container

In my case I tried both CMD and ENTRYPOINT command both are working as expected.Below is my screen for ENTRYPOINT command.

Next, we will create an image of my java application based on this docker command i.e. “docker build . ” Below is my screen.

We can also create a docker image with name tag, i.e. “docker build -t mhw .” , where “mhw” is my docker image name

Once this is done we can validate if the image got created or not by executing this command. i.e “docker images”. Below is my screen.

Next , we will run the docker image by executing this command i.e. “docker run <Image Id>”. Below is my screen, for simplicity I considered only 1st 3 characters of the container Id but you can provide entire container Id also.

Apart from this we can run our image using name tag also for that we need to execute this command i.e. “docker run mhw

The above image is created entirely using git bash. In case you are not familiar with git bash you can also use command prompt, in that case create a DockerFile in prior using notepad without any extension and place it in your code base and execute all the docker commands that I have mentioned.

Hope this will help developers who wants to create a docker image for their java application.The entire code snippet is available in the below git-hub link-https://github.com/mousumi8/Docker

I am just trying to share my experiences and open to questions and constructive criticism that will help me to create more valuable resources for other developers.Please share your feedback whoever reads this content, this will in a way encourage me.




Software engineer. Open to learn new things.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How Zenko help us on our journey to cloud agnosticity

Google playstore Errors & Solutions on Lenovo Vibe K5 Plus

Can't Install App (error Code : -504)

Classes and Objects

There Are Only 2 Reasons to Use a Coding Resource

VR in Unity: Managing Controller Input and Hand Presence (Part 1: Controller Set Up)

Email Integration in Spring Boot with a fake SMTP service — Ethereal

Cirrus CLI — CI-agnostic tool for running Dockerized tasks

PDF Download! CNC Programming using Fanuc Custom Macro B Read !book %ePub

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mousumi Hazarika

Mousumi Hazarika

Software engineer. Open to learn new things.

More from Medium

Github Actions: The Basics

github actions logo

Github Workflow | How powerful it can get! :P

Building a landing page with React, Flask and AWS in 2022 — A definitive guide — Part IV — Connect…

Check the console output for the API endpoint address

Want to get hired? Build REST app