dockerizing spring boot application maven

Brief Intro and advantages of dockerization 

Dockerizing an application means making our application run in a docker container. Dockerize an application is also means as containerize  an application.

 Why? Because it allows a developer to package the application and its dependencies and libraries as a single package. It will be easier to create, deploy and run the application. In short, it dockerizing helps to package, distribute and manage the applications inside the containers.

There are many advantages docker brings to the table. 

  • Portability - Once you have tested your dockerized application, you can deploy to any other system running the docker and it will run without any issues. No more - it runs only on my machine issues.
  • Resource usage and performance - docker uses host system kernel and resources as required by the container unlike VM which has an operating system and which uses same amount of resources even when applications doesn’t need them. No more wastage of resources. 
  • Isolation and scalability - Each docker container has its own lifecycle and can create new containers as per demand. They are faster to create and quicker to start. 
  • Security - As docker containers are isolated, this will give us more control over traffic management and security
  • Flexibility - configuration can be put into code and can be deployed to any environments without any problem. No need of setting up configs again and again. 

Let’s Dockerize

We will use the same code base which we used to develop email scheduling application using quartz scheduler in springbooot using mysql and postgres as database in the posts below respectively.

Let’s begin by writing the docker file. 


In this docker file, I am using ubuntu 16.04 as base image and installing JDK 8 and maven to build the jar and run the application with the help of file where we run the jar. 

Script to automate steps

First, we will check if an existing container is present. If it is present, we will stop the container and remove it. Also, we will delete the existing image. 

In the next step, using maven, we will build the project and generate a new jar and create a new image. 

Next, we will run the image with profile local-postgres and expose port 9166 to host machine. 

Postgres container should be running, before executing the file. 

Refer this post to learn how to run the postgres container. 

You can follow the same process for mysql too by creating a mysql container. Refer this post on how to create a mysql container. 

Here we are creating a container for database and binding it with the host port. And from the application container, we are interacting with the database container via same port on host. 

For docker v20.10 and above, add --add-host=host.docker.internal:host-gateway  to your docker command and in application, make sure to use - host.docker.internal in place of localhost to enable the container to access host machine ports. 

dockerize spring boot application with mysql

Refer this  stack overflow for other versions of docker engine -

If wondering how to see the logs printed to console from application, use below command 

docker logs <containerId> -f

This will follow the output much like tail -f command. 

spring boot microservices with docker example


We can add more steps to our like pushing the image, tagging our image. I will go through some of the commands. 

Pushing the image to docker registry

Now, you have docker image and to share it with the world, we need to create a account at 

 After verifying the email, we are ready to go and upload the image. 

Login to your docker hub from command line

docker login --username=your_docker_hub_username

Tag your image

docker tag <ImageId> yourusername/imagename:tag

Push to docker hub

docker push yourusername/imagename

Now, everyone can pull the image  by typing docker pull <ImageName>:tag.  Explore more and experiment. 

Resources for learning docker

Keep Experimenting 🔎 

Keep Learning 🚀

Post a Comment

Previous Post Next Post