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.

Prerequisite to create a docker image

1. You need to have docker install on your machine.
Please follow – https://docs.docker.com/install/#cloud

2. Have a docker file created which will have configurations to create docker image out of jar file.

Now that, we already have Docker installed on our machine and we also have a jar file created already, let’s see how we can create a docker file.

We will use the following configurations in docker file for our sample project.

Create a dockerfile and add it to the root of your project with the following configurations

FROM java:8
WORKDIR /
ADD /target/maven-app-1.0-SNAPSHOT-jar-with-dependencies.jar maven-app-1.0-SNAPSHOT-jar-with-dependencies.jar
EXPOSE 8080
CMD java -jar ./maven-app-1.0-SNAPSHOT-jar-with-dependencies.jar

Please follow the following link to create an account at DockerHub
https://hub.docker.com/

Docker commands

Docker commands to create an image and push it to dockerhub

$ docker build -t maven-app .
$ docker images
$ docker tag maven-app deepakmehra10/maven-app:latest
(by default)

You can also tag your docker image with any other name

For example

$ docker tag maven-app deepakmehra10/maven-app:v1

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.

docker images

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!

knoldus-advt-sticker

Written by 

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.

Leave a Reply

%d bloggers like this: