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.
- 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.
- WORKDIR command is used to set a working directory with directory name as my-hello-world directory. Note this step is completely optional.
- COPY command as the name suggest copy’s MyHelloWorld.java from the code base to this working directory.
- RUN command is used to install the JDK as alpine doesn't come with a JDK and also to compile the java file.
- ENV command is used to set the environmental variables.
- 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.