For more articles, visit my blog – code is endless.
Microservices are now very popular in Internet companies. In the past, when looking for a job, many HR people would ask if they had contact with microservices. Microservice and Docker can be perfectly combined to achieve the implementation of microservice architecture more conveniently. As a representative SpringBoot framework in microservices, today we will learn how to run a SpringBoot application in a Docker container.
Create the Spring Boot program
In this article we will run a simple SpringBoot Web application in a Docker container, and here is the initial pom.xml.
<?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>
<groupId>cn.itweknow</groupId>
<artifactId>springboot-docker</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-docker</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</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>
</plugins>
</build>
</project>
Copy the code
Add a helloController.java
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(a) {
return "Hello Docker."; }}Copy the code
Ok, so now we have a simple Web application that we can run locally and look at to make sure it works.
Configuration Docker
Docker provides a maven-built plugin called docker-maven-plugin. We just need to add this plugin to our pop.xml and do some simple configuration.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<! -- Here is the final generated docker image name -->
<imageName>itweknow/${project.artifactId}</imageName>
<! -- Base image, run a SpringBoot application just need the basic Java environment -->
<baseImage>java:8</baseImage>
<! -- Docker startup command -->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
Copy the code
Yes, it’s as simple as that. So far, our whole project has been built. The remaining work is to copy the project to our Linux environment to generate docker image and run it.
Build the mirror
Copy project to Linux, enter the project directory, execute the following commands in sequence, you can generate a docker image.
mvn clean
# -dmaven.test. skip=true
mvn package -Dmaven.test.skip=true
mvn docker:build
Copy the code
Of course, you can execute all three commands at once
mvn clean package docker:build -Dmaven.test.skip=true
Copy the code
Then execute docker images to check the Docker image in the system to see whether it is successfully generated.
REPOSITORY TAG IMAGE ID CREATED SIZE
itweknow/springboot-docker latest f03b689cfc33 10 seconds ago 660MB
Copy the code
Run the Docker container
# -d specifies background running
# --name specifies the container name
# -p 8080:8080 is used to map port 8080 of the container to port 8080 of the host in the following format: Host (host) port: container port
docker run -d --name test -p 8080:8080 itweknow/springboot-docker
Copy the code
Run docker PS to see the running container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
652fd3ccac89 itweknow/springboot-docker "Java - jar/springbo..."3 seconds ago Up 2 seconds 0.0.0.0:8080->8080/ TCPtest
Copy the code
Our project has been successfully run in docker container, we can visit http:// VM IP:8080/hello to test.
Conclusion language
This article is just a brief introduction to how to use Docker to deploy our Spring Boot project, the content of Docker, the following article we will slowly understand with you. The full implementation of this article can be found on Github.
PS: more than learning, code non-stop hoof! If you like my article, follow me!