Deploy Spring Boot application in Docker

In this tutorial, I will guide you 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 Boot Initializr Web to create a Spring Boot project:

Deploy Spring Boot application in Docker

Then open it using IntelliJ IDE:

Deploy Spring Boot application in Docker

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!” as follows:

Result:

Deploy Spring Boot application in Docker

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:

Deploy Spring Boot application in Docker

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

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

This is called 8u141-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/Docker.app/Contents/MacOS).

Here, we will use the VOLUME command:

  • 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.
  • The next part is to use Docker to build Docker Image from Dockerfile.

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

Deploy Spring Boot application in Docker

including the Docker Image we just created.

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

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

Deploy Spring Boot application in Docker


Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

4 thoughts on “Deploy Spring Boot application in Docker

Add Comment