docker+jenkins+github deployment soringboot project

Keywords: Linux Docker Maven Spring SpringBoot

Preface

Using jenkins to associate github to publish spring boot project is very simple. This article introduces the process of publishing with docker.

ps: the server of this article is centos7

Install docker

yum install docker  

After installation, you need to set it to start

systemctl  start docker.service
systemctl  enable docker.service

Then use the Chinese accelerator to download the image faster

vi  /etc/docker/daemon.json

#After adding:
{
    "registry-mirrors": ["https://registry.docker-cn.com"],
    "live-restore": true
}

Restart docker

systemctl restart docker

ps:jdk and maven also need to be installed. I will not add them again this time. For details, see my previous article:
https://segmentfault.com/a/1190000014060856

springboot project

1.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 https://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.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.iamcrawler</groupId>
    <artifactId>docker-demo</artifactId>
    <version>latest</version>
    <name>docker-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <docker.image.prefix>springboot</docker.image.prefix>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>docker-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</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>
            <!-- Docker maven plugin -->
        </plugins>
    </build>

</project>
        
        

2. Create a Dockerfile file under the directory src/main/docker. The Dockerfile file is used to illustrate how to build an image.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD docker-demo.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

The Dockerfile file is very simple. Build the Jdk basic environment, add Spring Boot Jar to the image, and explain briefly:

  • FROM, which means using the Jdk8 environment as the base image. If the image is not local, it will be downloaded FROM the DockerHub.
  • VOLUME, VOLUME points to a directory of / tmp. Because Spring Boot uses the built-in Tomcat container, Tomcat uses / tmp as the working directory by default. The effect of this command is to create a temporary file in the host's / var/lib/docker directory and link it to the / tmp directory in the container
  • ADD, copy file and rename
  • ENTRYPOINT, in order to shorten the start-up time of Tomcat, add the system attribute of java.security.egd to point to / dev/urandom as ENTRYPOINT.

3.controller

@RestController
@RequestMapping("/docker")
public class DockerDemoController {

    @GetMapping("/hello")
    public ResponseEntity demo(){
        return ResponseEntity.ok("hello docker");
    }
}

In this way, the Spring Boot project can add Docker dependency.

jenkins configuration

jenkins, after associating our github address, we choose to execute the shell.


#!/bin/bash 
##Find out the process and shut down
pids=$(ps -ef | grep docker-demo.jar| awk '{print $2}')
for pid in $pids
do
 echo  $pid
 kill -9  $pid
done
echo "process $pid Already been kill"
##Pack
mvn clean install -Dmaven.test.skip=true
echo "maven completion of enforcement"
cur=$(pwd)
echo "current path $cur"
cp $cur/target/docker-demo.jar /docker/jar/docker-demo.jar

dockerpids=$(docker ps -f name="docker-demo" | awk '{print $1}' )
for dockerpid in $dockerpids
do 
  echo "Deleted container $dockerpid"
  docker stop $dockerpid
  docker rm $dockerpid
done  

echo "Container deleted!"

ipids=$(docker images | grep "springboot/docker-demo" | awk '{print $3}')
for imagesid in $ipids
do
  echo "Deleted image $imagesid"
  docker rmi $imagesid
done  

echo "Old docker docker-demo Container and mirror deleted"

mvn clean package -Dmaven.test.skip=true docker:build
echo "docker Pack up"
docker run --name="docker-demo" -p 7779:7779 -t springboot/docker-demo
echo "Boot up"

At this time, we can see the result by visiting www.iamcrawler.cn:7779/docker/hello...

Posted by tester2 on Tue, 22 Oct 2019 03:13:00 -0700