In this video, I'm going to create and build Docker image for user's microservice.
I will select user's Microservice Spring Boot project and create a new file for it.
Provide file name.
I'll name it Docker file and click on Finish.
I have already prepared a Docker file, so I'll paste it here and I will walk you through every single
line in this file.
You already know we have done it in previous videos and it should be familiar to you.
So we are creating a Docker image for user microservice based on official docker image called Openjdk
Alpine.
And this is our first line from Openjdk eight JDK Alpine.
The Open JDK Alpine is based on a popular Alpine Linux project, and Alpine Linux project is much smaller
than most distribution based images.
It's about five megabytes only, and because it is very small, it leads to much smaller images.
So I'm going to use it.
I will then add the generated jar file which is used to start up users microservice, and I'm renaming
it to a simpler name users microservice jar file and on the last line, this jar file will be executed
with Java Jar Command and when executing the Java Jar Command, I also pass in one system property which
we also already talked about in one of the earliest video.
And this system property is not necessary for most of spring boot applications, but for those applications
that use encryption and generate tokens Passing this system property to use your random instead of random
makes your application work with random numbers slightly faster.
So I'm including this property as well.
The Docker file is very small and simple.
I will save it and I'm ready to build it.
I will select my project one more time, open Properties and will select the location of this project
and we'll bring in a terminal window.
I will change directory to the location of this project and will first rebuild this project.
Using Maven, I will run Maven Clean Command to clean the target folder first.
And now I will run Maven package command to package this project into executable jar file and hit enter.
Now, when the project is built, I will list files in the target folder.
And I want to make sure that the name of generated jar file matches the name in my Docker file.
So I'll close this.
So the name of this jar file that I'm adding to a Docker image should be exactly as the name in the
generated target folder.
So now I'm ready to build this Docker file.
I'll go back to a terminal window and I will use Docker Build command to build this Docker image.
I'll use Docker build dash dash tag and I can actually tag it with the repository name right away so
that after the build is successful, I can push this Docker image to Docker Hub.
So I'll use dash dash tag equals and then repository name.
Let me create repository name first.
So I'll go to a browser window.
Here's my Docker hub page and I'll create a new repository.
And this repository will be for users microservice, I'll call it SRC dash users, dash microservice
like this and I'll create.
All right, so the repository is created.
I will copy its name and go back to my terminal window and I will tag it with the name that first starts
with a Docker hub username.
My username is kargopol and then forward slash and the name of the repository.
All right then.
Space dash.
Dash, force dash to request intermediate containers to be deleted after the command is done equals
true.
And then space and dot at the end.
Hit enter.
And the Docker image is successfully built.
Now we can push it to Docker Hub.
I will copy the name of the tag and I will use Docker, push and paste the name of the tag of this Docker
image and hit enter.
All right.
The push is successful and it should be able to pull this Docker image from Docker Hub and run it on
my EC2 instance.
Let's do that.