Dockerizing any Spring Boot application

In this section, I'll focus on how to dockerize a Spring Boot application (Account-Service) to run in an isolated environment, which is a container. In the previous chapters, we have created some microservices, such as Account-Service and Customer-Service. Now I will describe the process of migrating a Spring Boot Account-Service to Docker. We will start by modifying a build file, then we will create Dockerfile so it can be run locally.

So let's go ahead and create Dockerfile in our Spring Boot project:

#This is a Dockerfile for a microservice application 
 
# Use an official Java 8 runtime as a parent image 
FROM maven:3.5-jdk-8-alpine 
 
VOLUME /tmp 
 
#Set maintainer email id 
MAINTAINER [email protected]  
 
# Set the working directory to /app 
WORKDIR /app 
 
# Copy the current directory contents into the container at /app 
ADD . /app 
 
# Build and create jar using maven command 
#RUN mvn package -DskipTests=true -Ddir=app 
 
# Copy the current directory contents into the container at /app 
ADD target/account-service-0.0.1-SNAPSHOT.jar accounts-microservice.jar 
 
# Make port 80 available to the world outside this container 
EXPOSE 80 
 
# Define environment variable 
ENV JAVA_OPTS="" 
 
# Run accounts-microservice.jar when the container launches 
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar accounts-microservice.jar" ] 

The preceding Dockerfile is very simple, but that file has all you need to run a Spring Boot application and create a JAR file using the maven command mvn package. The project JAR file is added to the container as accounts-microservice.jar and then executed in ENTRYPOINT.

Let's see the following screenshot of this application directory structure:

As you can see, Dockerfile has placed the application on a directory parallel to the pom.xml file. Now, let's create a Docker image by using the following command:

$ docker build -t spring-boot-app  .

spring-boot-app is the name of the image. We can give our Docker image any name. The preceding command will build the image from Dockerfile, we need to specify the Dockerfile path. In the previous command, we mentioned "." at end of the command, its means Dockerfile is located in the current working directory.

The -t option is for tagging, it tags the new image followed by the version:

$ docker build -t spring-boot-app:1.0.1  .

Let's see the following screenshot of the output of preceding command:

As you can see, the Docker image has been created successfully. Now, we can run our Docker image using the docker run command. The following command is used to run spring-boot-app:

$ docker run -p 8080:8080 spring-boot-app:latest

Let's see the following screenshot of the preceding docker run command to run the container of the created spring-boot-app Docker image:

As you can see, our Account-Service has been run successfully. Now, we can see that after running spring-boot-app, it produced on the browser by accessing the following URL:

http://192.168.99.100:8080/account

As you can see in the preceding screenshot, it is rendering data access from the H2 DB, as we discussed in previous chapters. Now, our ACCOUNT-SERVICE microservice has been dockerized and is running as a Docker container. http://192.168.99.100 is the IP of the container to access it outside, and 8080 is the port of this particular container.

So far, we have created a microservice and built it as a Docker image by using the docker build command. We can also create a Docker image by using Maven or Gradle. Let's see the following section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.77.63