Spring Boot Integration with Docker
What is Docker?
Docker is an open-source engine that allows developers to create lightweight, portable, and self-contained containers for any application. With Docker, developers can test and deploy containers in a variety of environments, including virtual machines, bare metal, OpenStack clusters, and more.
Docker Application Scenarios
- Automated Packaging and Publishing Web Applications: Docker can automate the packaging and publishing of web applications, making it easier to deploy and manage applications.
- Automated Testing and Continuous Integration: Docker can automate testing and continuous integration, ensuring that applications are thoroughly tested and validated before deployment.
- Deployment and Tuning of Database or Other Back-Office Applications: Docker can deploy and tune database or other back-office applications in a service-oriented environment.
- Recompiling or Extending an Existing OpenShift or Cloud Foundry PaaS Platform: Docker can recompile or extend an existing OpenShift or Cloud Foundry PaaS platform to build a custom environment.
Integration of Docker with Spring Boot
To integrate Docker with Spring Boot, we need to create a Spring Boot project and then containerize it using Docker.
Create a Spring Boot Project
Let’s create a Spring Boot project called springboot-docker. We will create a Start class with a main method that runs the Spring Boot application.
package com.gf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringbootDockerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDockerApplication.class, args);
}
@GetMapping("/hi/{name}")
public String hi(@PathVariable(value = "name") String name) {
return "hi," + name;
}
}
Containerize the Spring Boot Application using Docker
To containerize the Spring Boot application using Docker, we need to create a Dockerfile in the src/main/resources/docker directory. The Dockerfile will specify the base image, volume, add the Spring Boot application JAR file, and set the entry point.
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD springboot-docker-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar"]
Build the Docker Image using Maven
To build the Docker image, we need to add the docker-maven-plugin to the pom.xml file. The pom.xml file will specify the project metadata, dependencies, and plugins.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>springboot-docker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-docker</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<docker.image.prefix>gf</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/resources/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Build the Docker Image
To build the Docker image, we need to run the following commands:
mvn clean
mvn package docker:build
View the Docker Image
To view the Docker image, we need to run the following command:
docker images
Run the Docker Container
To run the Docker container, we need to run the following command:
docker run -p 8080:8080 -t c2dba352c3c1
We can now access the service by navigating to http://localhost:8080/hi/John.