Deploy Spring Boot application in Docker

In this tutorial, I will guide you on how to deploy a Spring Boot application in Docker.

The steps, we need to take, are:

  • Create a simple Spring Boot project.
  • Write Dockerfile
  • Deploy the application to the Docker Container.

OK, let’s get started.

Create a Spring Boot project

I will use Spring Tool Suite to create a Spring Boot project with Web, Data Jpa Starter, and PostgreSQL driver dependencies as follows:


For simplicity, I will open the SpringBootDockerApplication file:

And modify it a little bit to add a RESTful Web Service return the string “Hello Docker! Idle connection to database is 10” as follows:

By default, Spring Data Jpa uses Hikari for the connection pool and as you can see, I use Hikari to get the idle connection to the database.

The database configuration is configured in the file as follows:

Result when I run the program with:

as follows:

Now, I will write a Dockerfile to deploy our application to the Docker Container.

Write Dockerfile

I will create a new Dockerfile file located in the project directory:

Now, we will edit this Dockerfile to build a Docker Image.

  • First, I will use an Image on the Docker Hub to build a Docker Image for us.

This is called 11.0.11-jre located in the openjdk repository. Therefore, your FROM statement will have the following contents:

  • Next, we will link the /tmp directory of the Docker Container to the Docker directory (/Applications/

Here, we will use the VOLUME command:

  • To run the application, you need to declare some environment variables related to the database configuration as above:

Here, I am using a PostgreSQL database server installed on my machine, so I declare the database host as host.docker.internal.

  • Now I will copy the spring-boot-docker-0.0.1-SNAPSHOT.jar file located in the target directory of the project into the Docker Container.

  • And finally, executing the command to run our Spring Boot application every time our Docker Container is run.

The overall content of the Dockerfile is as follows:

Deploy the application to the Docker Container

To deploy the Spring Boot application to the Docker Container from the Dockerfile, we first have to create the Docker Image from the Dockerfile.

Open the terminal and go to the project directory, then enter the following:

The above statement has two parts:

  • The first part is that we will use Maven to build the Spring Boot application, I skip all unit tests because it is not the goal of this tutorial.
  • The next part is to use Docker to build Docker Image from Dockerfile.

After running the above commands, if you check all Docker Images, you will see our Docker Image as follows:

And now, we can run the Docker Container from the Docker Image we just created.

The result will be:

Here, I have mapped the port of our machine with port Docker Container is 8080, so to check the results, you can visit http://localhost:8080/hello.

5/5 - (1 vote)

4 thoughts on “Deploy Spring Boot application in Docker

Add Comment