Spring Boot integration docker

I. what is docker?

brief introduction

Docker is an open source engine, which can easily create a lightweight, portable, self-sufficient container for any application. The containers that developers compile and test on laptops can be deployed in production environment in batches, including VMs (virtual machine), bare metal, OpenStack cluster and other basic application platforms.

Application scenario of docker

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

II. Integrating docker

Create project

Create a springboot project springboot docker

1. boot class

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. Container the springboot project

We write a Dockerfile to customize the image and create a Dockerfile 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 docker image through maven.
Add the plug-in built by docker image to the pom directory of maven

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

Constructing mirrors

Let's run the following command to build the image:

mvn clean
mvn package docker:bulid

After the construction is successful, we can view the image through the following command:

docker images

Start mirror:

#c2dba352c3c1 is the image ID
docker run -p 8080:8080 -t c2dba352c3c1

Then we can access the service.

Source code download: https://github.com/gf-huanchupk/SpringBootLearning

Pay attention to my public address.

Keywords: Linux Docker Maven SpringBoot Spring

Added by ClyssaN on Thu, 05 Dec 2019 11:10:18 +0200