DevOPS has gradually become the industry standard and has been accepted by more and more people. It is difficult to mention DevOPS without mentioning Docker. In this paper, Gitlab CI and Docker are used for CI/CD


DevOps (a portmanteal of Development and Operations) is a culture, movement, or practice that values communication and cooperation between “software developers” and “IT Operations technicians”. Build, test, and release software faster, more frequently, and more reliably by automating the software delivery and architecture change processes.

What you’ll learn:

  • Docker installation

  • Containerize Spring Boot

  • Gitlab installation

  • Gitlab CI setup

Docker installation

The following uses CentOS as an example. For other systems, see the official documents

Installing dependency packages


     

    $ sudo yum install -y yum-utils \

     device-mapper-persistent-data \

     lvm2

Copy the code

Setting the Installation Source


     

    $ sudo yum-config-manager \

       --add-repo \

       https://download.docker.com/linux/centos/docker-ce.repo

Copy the code

Install the Docker – CE


     

    $ sudo yum install docker-ce

Copy the code

Start docker-CE and test it


     

    $ sudo systemctl start docker

    $ sudo docker run hello-world

Copy the code

Set accelerator

The first step for us to use Docker should be to obtain an official image, such as mysql and wordpress. Based on these basic images, we can develop our own personalized applications. We can use the Docker command line tool to download the official image. However, due to network reasons, we need a long time to download a 300M image, and even the download failed. For this reason, Ali Cloud container Hub service provides an official mirror site to speed up the download of official images.

Address: https://yq.aliyun.com/articles/29941

Use the accelerator by modifying the daemon configuration file /etc/dock/daemon. json:

                        
     

    $ sudo mkdir -p / etc/docker

    $ sudo tee /etc/ docker/daemon .json <<- 'EOF'

    {

    "registry-mirrors": ["https://7u8fl13h.mirror.aliyuncs.com"]

    }

    EOF

    $sudo systemctl daemon -reload

    $sudo systemctl restart docker

Copy the code

Containerize Spring Boot

Here we assume that our development language is Java, using the SpringBoot framework.

  1. Creating a project structure

*nix系统下: mkdir-p src/main/java/hello

  1. Generate the pom. XML


     

    <? The XML version = "1.0" encoding = "utf-8"? >

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

    The < modelVersion > 4.0.0 < / modelVersion >

       <groupId>org.springframework</groupId>

       <artifactId>gs-spring-boot-docker</artifactId>

    < version > 0.1.0 from < / version >

       <parent>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-parent</artifactId>

    . < version > 2.0.3 RELEASE < / version >

       </parent>

       <properties>

    < Java version > 1.8 < / Java. Version >

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

       <repositories>

           <repository>

               <id>alimaven</id>

               <name>aliyun maven</name>

               <url>http://maven.aliyun.com/nexus/content/groups/public/</url>

           </repository>

       </repositories>

       <build>

           <plugins>

               <plugin>

                   <groupId>org.springframework.boot</groupId>

                   <artifactId>spring-boot-maven-plugin</artifactId>

               </plugin>

           </plugins>

       </build>

    </project>

Copy the code
  1. Edit SRC /main/Java /hello/application.java


     

    package hello;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RestController;

    @SpringBootApplication

    @RestController

    public class Application {

       @RequestMapping("/")

       public String home() {

           return "Hello Docker World";

       }

       public static void main(String[] args) {

           SpringApplication.run(Application.class, args);

       }

    }

Copy the code

Testing:


     

    $MVN package && java-jar target/gs-spring-boot-docker-0.1.0.jar

Copy the code
  1. The container is changed

Add Dockerfile

                                
     

    FROM openjdk :8- jdk-alpine

    ARG JAR_FILE

    COPY $ {JAR_FILE} app.jar

    ENTRYPOINT ["java", "-jar", "/app.jar"]

Copy the code
  1. Adding Maven support

Edit the pom. XML

                                    
     

    <properties>

    springio

    </properties>

    <build>

    com.spotify

    dockerfile-maven-plugin

    The < version > 1.3.6 < / version >

    ${docker.image.prefix}/${project.artifactId}

    target/${project.build.finalName}.jar

    </build>

Copy the code

     

    $ mvn install dockerfile:build

Copy the code
  1. run


     

    $ docker run -p 8080:8080 -t springio/gs-spring-boot-docker

Copy the code

conclusion

Now we have docker-like our project, we upload the project to our Gitlab to complete the preparation of the development project

Gitlab installation

We can install Gitlab with one click through Docker. The installation commands provided by the official document are as follows:


     

    sudo docker run --detach \

       --hostname gitlab.example.com \

       --publish 443:443 --publish 80:80 --publish 22:22 \

       --name gitlab \

       --restart always \

       --volume /srv/gitlab/config:/etc/gitlab \

       --volume /srv/gitlab/logs:/var/log/gitlab \

       --volume /srv/gitlab/data:/var/opt/gitlab \

       gitlab/gitlab-ce:latest

Copy the code

Simplified version


     

    docker run --detach \

       --hostname git.1programmer.com \

       --publish 17880:80 \

       --name gitlab \

       --restart always \

       gitlab/gitlab-ce:latest

Copy the code

Gitlab CI

We want to make CI work, and we need to do two things

  1. Add the.gitlab-ci.yml file to your project root

  2. Set the Runner

.gitlab-ci.yML simple preparation

The simplest CI file to build Docker image


     

    build:

     stage: build

     script: mvn install dockerfile:build

    dev:

     stage: deploy

     script:

     - docker rm -f springio/gs-spring-boot-docker || true

     - docker run -d -p 11080:8080 -t springio/gs-spring-boot-docker

Copy the code

Gitlab Runner installation


     

    # # set the source

    $ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash

    # # to install

    $ sudo yum install gitlab-runner

Copy the code

Gitlab Runner configuration


     

    $ gitlab-runner gitlab-runner register

    Running in system-mode.

    Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):

    # set target Gitlab address here, for example: http://git.1programmer.com/

    Please enter the gitlab-ci token for this runner:

    The token needs to be obtained from the configuration in the project

    Please enter the gitlab-ci description for this runner:

    # description

    Please enter the gitlab-ci tags for this runner (comma separated):

    The # tag can be skipped

    Whether to lock the Runner to current project [true/false]:

    # Whether to lock to the current project, if it is a universal Runner can be shared by multiple projects, safety consider one Runner per project

    Please enter the executor: docker, shell, ssh, docker+machine, kubernetes, docker-ssh, parallels, virtualbox, docker-ssh+machine:

    # here we choose shell

Copy the code

Add user gitlab-runner to docker group to enable user gitlab-runner to operate docker

                                        
     

    $ usermod -a - G docker gitlab-runner

Copy the code

conclusion

This is the entire automated build process based on Gitlab CI, which is set up for continuous deployment of our project.

More original dry goods, please focus on thinking into today!

Identify the QR code, follow us

Some thought into this

Research and development Services | Consulting services | Testing services