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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

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.

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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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. By nature easy to approach and fun loving. Open to learn new things.

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