14. Implement CI and CD in Docker

Table of contents

1 Introduction

2. What is CI/CD

3. Deploy Jenkins

3.1. Download Jenkins

3.2. Start Jenkins

3.3. Visit the Jenkins page

4. Jenkins deploys an application

5. Jenkins implements continuous integration and deployment of Docker applications

5.1. Create Dockerfile

5.2. Integrate Jenkins and Docker

6. Summary


1 Introduction

Continuous Integration (CI/CD) is a summary of experience in software development. It is used to help development teams and delivery teams achieve frequent and rapid integration, as well as test their work products to find errors in project development and delivery engineering as quickly as possible. The more frequent and earlier the project integration and project delivery, it means the earlier the problem will be found. Therefore, through continuous integration (CI/CD), code faults can be discovered and resolved in a timely manner, code quality can be improved, and fault handling costs can be reduced.

2. What is CI/CD

CI/CD is a method for delivering applications to customers frequently by introducing automation in the application's development phase. CI/CD includes the following 3 concepts:

  • CI (Continuous Integration): continuous integration.
  • CD (Continuous Delivery): Continuous delivery.
  • CD (Continuous Deployment): Continuous deployment.

As an open source and the most used continuous integration tool, Jenkins is also widely used in projects. Let's take Jenkins as an example to introduce today.

3. Deploy Jenkins

Since Jenkins is developed based on the Java language, it is necessary to install the JDK environment, and JDK8 is installed here.

3.1. Download Jenkins

You can download Jenkins.war directly from the official website and upload it to the server. Jenkins download and deployment

It should be noted that the JDK version supported by jenkins can be seen from the official documentation of the java environment required by several LTS versions.

Since our JDK environment is java8, the war package of version 2.346.3 is downloaded here.

Quick download address: Index of /war-stable/2.346.3

wget --no-check-certificate https://get.jenkins.io/war-stable/2.346.3/jenkins.war

Here, due to the mirroring problem, you need to try a few more times.

3.2. Start Jenkins

After downloading, start Jenkins:

java -jar jenkins.war

If the following information is output, the startup is successful.

3.3. Visit the Jenkins page

Browser access http://192.168.74.128:8080/ .

The subsequent Jenkins installation steps are ignored here, there should be many tutorials.

After the installation is complete, go to the Jenkins main page.

4. Jenkins deploys an application

I won’t go into details about the traditional Jenkins construction application here. Here is the Docker column, so we will focus on the following section to realize the continuous integration and deployment of Docker applications.

5. Jenkins implements continuous integration and deployment of Docker applications

In Docker application development, the most common is to use the Dockerfile file, which can be managed by the code repository.

Generally speaking, the code warehouses in enterprises are all private, and departments will build their own private code warehouses, such as SVN, GitLab, etc. It is convenient to demonstrate here, so I will not build it myself, and use Github to demonstrate directly (mainly lazy).

To deploy the Jenkins server, Git must be installed in advance.

5.1. Create Dockerfile

Create a Dockerfile locally, which implements a simple nginx page:

FROM nginx
RUN echo '<h1>this is jenkins build demo,version:1</h1>' > /usr/share/nginx/html/index.html

Commit the file to the Github repository.

At the same time, configure the webhook in the Settings in the warehouse, and configure the address of our Jenkins for the Payload URL, which is used to send an http request to Jenkins every time we submit the code.

5.2. Integrate Jenkins and Docker

Create a Freestyle project task in Jenkins.

Configure Git-related information in source code management.

Then in the build options, select "Execute shell".

Enter the framework container command in the command dialog:

docker build -t my_jenkins_docker_demo .
docker run -d -p 8081:80 my_jenkins_docker_demo

After clicking Save, click Build Now.

After the build is successful, visit the address: http://192.168.74.128:8081:

6. Summary

So far, the integration of Jenkins and Docker has been completed, so that CI/CD of the application can be realized. In actual project development, a large amount of program code, as well as dependent libraries and environments may be involved, but the process of using Jenkins to implement CI/CD is similar.

Guess you like

Origin blog.csdn.net/p793049488/article/details/132536397