Jenkins + docker compose one click publishing of spring cloud service

Posted by Dillenger on Wed, 23 Oct 2019 09:24:26 +0200

Preface

The previous article described using docker+jenkins to publish the spring boot project on github. However, once multiple services need to be published together, one configuration for one is too tired to publish. Is there any way to package multiple docker images at one time, and then deploy the image file as a running container? Docker compose solves this problem perfectly. This article uses the spring cloud project as an example.

What is docker compose

Let's briefly introduce the definition of docker compose:
Docker Compose is the last of docker's three swordsmen. The first is Machine, which is used to quickly install docker on different platforms. The second is Swarm, which helps docker run in clusters. The third is Docker Compose, which is used to help users run container groups (please note that it is not a separate container).

Spring cloud configuration

ps:
   1.Need to install docker-componse. I have installed it here. The specific installation tutorial will not be described here. After installation, the specific viewing method is as follows:

   [root@iamcrawler /]# docker-compose -version
   docker-compose version 1.24.1, build 4667896
   
   2.This tutorial github Address: https://github.com/iamcrawler/micro.git

The first step has little to do with Docker Compose. Install Image. In the case, we use docker-maven-plugin to operate, and then write a build-image.sh script to complete the Image installation of all projects with one click.

The configuration of docker Maven plugin is as follows:

   <!---- eureka For example:--->
 <build>
        <finalName>crawler-eureka</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>

Step 2: the content of build-image.sh will not be pasted. Basically, you can enter the directory of each sub service and execute it in turn.

mvn clean package -Dmaven.test.skip=true docker:build

To complete the Image installation.

Step 3: write the docker-compose.yml file and package the image

docker-compose.yml should be located in the root directory of the project, and there should be no deviation in naming.
When writing docker-compose.yml, we should pay attention to that the container group to be defined is under services, and the Id of each service is generally consistent with the artifactId of the subproject, under which we can define the Image to be used, whether to restart in case of startup failure, container name, host name, port number to listen to and open, dependent services, etc.

image: ${What you have adopted Image}
restart: ${Whether to restart in case of failure, yes always}
container_name: ${Container name at run time}
hostname: ${The host name of the configuration network, which can be used for communication within the container group}
ports: 
  - "1234:1234" ${Listening and open port number}
depends_on:
  - docker-compose-eureka ${Dependent services}

Then the docker-compose.yml in the case will not be pasted. There are many, please go to the project to check.

Step 4: start the container

docker-compose up -d

-d indicates that the daemons are enabled

Follow up: we can close the container

docker-compose down

Finally, we put the process on jenkins:


##Pack
mvn clean install -Dmaven.test.skip=true
echo "maven completion of enforcement"
cur=$(pwd)
echo "current path $cur"

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

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

echo "Container deleted!"

ipids=$(docker images | grep "springcloud/*" | 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"

sh build-image.sh

echo "docker Pack up"


docker-compose up -d


echo "Container start complete,End."

For reference: https://www.liumapp.com/articles/2018/04/19/1524100110011.html

Topics: Linux Docker Maven Spring github