How to create your first docker images and run it in a docker container via command line?

So, Don’t you hate it when you are into deployments and don’t know how to go about docker and stuff. Today, we will talk about creating docker images and running it in a container via docker command line. It might sound frustrating and not easy but It’s possible and I will show you how.

To make things easier from the beginning I have divided the problem statement into 3 steps.

Step1. We will be making a simple program and create a Jar file out of it.

Step2. We will be creating the docker image from this Jar file and push it to dockerhub.

Step3. We will run the docker image via command line on our local machine.

Come, let’s do it. If you still feel it’s challenging than let me tell you It’s easier than you think.

Let’s implement Step1 first, Herein we are creating a small java program which will be adding two numbers. Once we are done with the program, we will be creating a Jar file out of this by using “maven assembly plugin”. Once everything is setup. You can run the following command to test if you have managed to produce a Jar file in the target folder.

Command to produce the jar file

mvn package

Once you run the above command a jar file will be created and you can find it in the target folder. This jar file will be used to create docker images which come in the
Step no 2. Please follow Step no 2 for docker image.

I will be adding the dummy project link, in the end, to make it quicker for you.

P.S – It’s going to be a maven project.Step2. Create a docker image and push it to the dockerhub.In order to create a docker image from a jar file, you will first need to install docker on your local and then create the docker file to create a docker image out of the jar file.

where deepakmehra10 is the dockerhub username.
And maven-app:v1 is the app name.

$ docker push deepakmehra10/maven-app:v1

where deepakmehra10 is the dockerhub username.
And maven-app:v1 is the app name.

Step 3. Once everything is setup, we will finally run the docker image using the following docker command

Command to run docker image –

$ docker run -it maven-app

Below is the output from the sample project.

If you are having any challenge building the app, you can access the full code at this link on Github. You can simply clone the repo and start experimenting.
Should you have additional questions and queries, please write in comments. If you have enjoyed this post, I would be very grateful if you would help it spread. Keep smiling, Keep coding! Cheers!

Share the Knol:

Related

Deepak is a Software Consultant having experince of more than 5 years . He is very enthusiastic towards his work and is a good team player. He has sound knowledge of different technologies which include Java, C++, C, HTML, CSS, Javascript, C# always keen to learn new technologies.

Knoldus is the world’s largest pure-play Scala and Spark company. We modernize enterprise through
cutting-edge digital engineering by leveraging Scala, Functional Java and Spark ecosystem. Our mission is to provide reactive and streaming fast data solutions that are message-driven, elastic, resilient, and responsive.

Knoldus is the world's largest pure-play Scala and Spark company. We modernize enterprise through
cutting-edge digital engineering by leveraging Scala, Functional Java and Spark ecosystem. Our mission is to provide reactive and streaming fast data solutions that are message-driven, elastic, resilient, and responsive.