1. Introduction to Docker
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, and then publish them to any popular Linux machine, or virtualize them. Containers use sandboxing mechanism completely and there will be no interface between them. Docker image is a solution for running containerized processes. In this article, we will build a simple Spring Boot application.
2. Environment construction
JDK 1.8+
Maven 3.0+
Docker latest version.
3. Build a project with Maven
3.1 Create a directory structure
mkdir -p src/main/java/com/lidong/demo
In Linux or Mac systems.
3.2 Create a pom.xml file
<?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>com.lidong.demo</groupId> <artifactId>lidong-spring-boot-demo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> </parent> <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>springio</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependency> </dependency> </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>0.4.13</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build></project>Notice:
Spring Boot Maven plugin provides many convenient features:
1) It collects all jar files on the classpath and builds into a single, runnable jar, which makes it more convenient to execute and transfer services.
2) It searches for the public static void main() method to mark as a runnable class.
3) It provides a built-in dependency parser for setting the version number to match Spring Boot's dependencies. You can override any version you want, but it will default
Selected version set of Boot.
Spotify's docker-maven-plugin plugin is a Docker Image used to build Maven
1) imageName specifies the name of the image. This example is springio/lidong-spring-boot-demo
2) dockerDirectory specifies the location of the Dockerfile
3) Resources refers to files that need to be put together with Dockerfile and used when building images. Generally, application jar packages need to be included.
4. Write the first Spring Boot application
Write a simple Spring Boot application:
src/main/java/com/lidong/demo/SampleController.java:
package com.lidong.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @Project name:lidong-dubbo * @Class name:SampleController * @Class description: * @Author:lidong * @Creation time: 2017/2/19 9:34 am * @Company:chni * @QQ:1561281670 * @Email:[email protected] */@Controller@SpringBootApplicationpublic class SampleController { @ResponseBody @RequestMapping(value = "/") String home(){ return "Hello Docker World"; } public static void main(String[] args) { SpringApplication.run(SampleController.class,"--server.port=8081"); }}
The class is identified by @SpringBootApplication @RestController, and can be used to handle web requests by Spring MVC.
@RequestMapping Map / to home() and respond with "Hello Docker World".
The main() method uses Spring Boot's SpringApplication.run() method to start the application.
5. Run the program
5.1 Use the Maven command
mvn package
run:
java -jar target/lidong-spring-boot-demo-1.0-SNAPSHOT.jar
Visit the project
If the program runs correctly, the browser visits http://localhost:8081/ and you can see the words "Hello Docker World." on the page.
5.2 Using IDEA plug-in
6. Container the project
Docker uses the Dockerfile file format to specify the image layer.
Create the file src/main/docker/Dockerfile:
FROM frolvlad/alpine-oraclejdk8:slimVOLUME /tmpADD lidong-spring-boot-demo-1.0-SNAPSHOT.jar app.jarRUN sh -c 'touch /app.jar'ENV JAVA_OPTS=""ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
Explain this configuration file:
VOLUME specifies the temporary file directory as /tmp. The effect is to create a temporary file in the host /var/lib/docker directory and link it to the container's /tmp. The modification steps are optional, and it is necessary if it involves application of the file system. The /tmp directory is used to persist to the Docker data folder, because the embedded Tomcat container used by Spring Boot uses /tmp as the working directory by default.
The project's jar file is added to the container's
ENTRYPOINT Execute project app.jar. To shorten Tomcat startup time, add a system property to point to "/dev/urandom" as Entropy Source
Build Docker Image
Execute the build to become docker image:
mvn package docker:build
run
Run Docker Image
docker run -p 8081:8081 -t springio/lidong-spring-boot-demo
See this Spring icon. I thought that we have completed the Spring boot program on docker.
Next, go to the browser and visit http://localhost:8081/, and you can see the words "Hello Docker World." on the page.
Summarize
The above is the spring Boot application introduced to you by the editor. The spring Boot application is built, run and release through Docker. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!