SpringBoot integrates docker

SpringBoot integrates docker

1. What is docker?

Introduction

Docker is an open source engine that can easily create a lightweight, portable, and self-sufficient container for any application. Developers can compile and test containers on their laptops and deploy them in a production environment in batches, including VMs (virtual machines), bare metal, OpenStack clusters, and other basic application platforms.

Application scenarios of docker

  • Automatic packaging and publishing of web applications;
  • Automated testing and continuous integration and release;
  • Deploy and adjust databases or other background applications in a service-oriented environment;
  • Compile or extend the existing OpenShift or Cloud Foundry platform from scratch to build your own PaaS environment.

For the use of docker, I will have a special topic to explain in detail later .

2. integrate docker

Create project

Create a springboot project springboot-docker

1. Startup

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("/{name}")
	public String hi(@PathVariable(value = "name") String name) {
		return "hi , " + name;
	}


}
 

2. Containerize the springboot project

We write a Dockerfile to customize the image, create a Dockerfile file under src/main/resources/docker

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" ]
 

3. pom.xml

We build the docker image through maven. In the pom directory of maven, add the plug-in built by the docker image

<?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 image

We run the following command to build the image:

mvn clean
mvn package docker:bulid
 

After the build is successful, we can view the image with the following command:

docker images
 

Start the mirror:

#c2dba352c3c1  ID
docker run -p 8080:8080 -t c2dba352c3c1
 

After that we can access the service.

Source download: github.com/gf-huanchup...