Search Tutorials

Deploying Spring Boot Microservice to Docker | JavaInUse

Deploying Spring Boot Microservice to Docker

Docker Tutorial - Table Of Contents

Docker Deploying Spring Based WAR Application to Docker Deploying Spring Based JAR Application to Docker Deploying Multiple Spring Boot Microservices using Docker Networking Deploying Multiple Spring Boot Microservices using Docker Compose Deploying Spring Boot + MYSQL Application to Docker Publishing Docker Image to DockerHub Deploy Docker Swarm services using Play With Docker Deploy Docker Swarm services using Docker Stack Deploy Docker Swarm services to multiple AWS EC2 instances Docker Cheat Sheet


This tutorial is explained in the below Youtube Video.

In this example we will -
  • Create a Spring Boot Web Application.
  • Create image for starting this application.
  • Run the above image as container to start the jar.
In a previous tutorial we had created a simple Spring Boot Web Application named employee-producer, that exposed a REST web service. We could retrieve employee information in JSON format using this REST service. This was developed to be deployed using JAR packaging.
Unlike previous tutorial where we used external tomcat,we will be using embedded tomcat for running this application. The project we will as follows-
Run maven command - clean install, and a jar file gets created in the target folder. Next we will start docker and deploy this jar using docker.

Create the dockerfile. Docker file is a list of commands that we want the docker engine to execute. Go to the spring boot project folder and create a docker file as follows-
From openjdk:8
copy ./target/employee-producer-0.0.1-SNAPSHOT.jar employee-producer-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","employee-producer-0.0.1-SNAPSHOT.jar"]

Open the terminal and start the docker
systemctl start docker

Now open the terminal and go to the Spring Boot project folder.
Next we will build an image with the name producer.
docker image build -t employee-producer .

Next we will run the above image as a container. Also we will be publishing the docker port 8080 to centos port 8080.

This command is same as the previous examples only difference is we will be using an additional command parameter name.
Until now we made use of the unique container id when we created a new container. Instead now we can give an name to the container and use it for referring the container.
docker container run --name producer -p 8080:8080 -d employee-producer


Here we have started the container with name as producer. Now using the following command check the logs
docker container logs producer

The application has started successfully. Go to localhost:8080/employee, we will see that our application is deployed successfully.

Download Source Code

Download it - Spring Boot Jar Application To Docker