Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a containerized build environment using Jenkins Pipeline plug-ins and Docker

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use Jenkins Pipeline plug-ins and Docker to build a containerized construction environment, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

The combination of Docker and Jenkins, like chocolate and peanut butter in DevOps, creates countless opportunities and, of course, a lot of problems, both of which I will mention.

I assume that readers are already familiar with Jenkins and Docker, and I will focus on specific configurations rather than spending my pen and ink on the introductory concepts that have been introduced in many blog posts.

Set goals

The goal I want to achieve is actually very simple: build the Jenkins master node in one container and build multiple JNLP proxy containers on multiple hosts. These proxy nodes can run on different AWS VPC or ECS.

My goal is to get a common configuration that can be deployed on any host, and each project defines its own build environment. This allows individual development teams to control the configuration without going through Jenkins's build team. I will try to avoid building a proxy node for a specific toolset. Container technology can achieve such a build environment, but it is definitely a challenge to do every detail well.

To achieve this goal, I also used the Jenkins Pipeline / Workflow plug-in. This plug-in allows you to describe the build process very elegantly in the DSL language, such as this simple definition:

```js

Node ('test-agent') {

Stage "Container Prep"

/ / do the thing in the container

Docker.image ('maven:3.3.3-jdk-8'). Inside {

/ / get the codez

Stage 'Checkout'

Git url: 'https://github.com/damnhandy/Handy-URI-Templates.git'

Stage 'Build'

/ / Do the build

Sh ". / mvnw clean install"

}

}

`

This pipeline will be executed on a Jenkins agent named "test-agent", and it will build a container based on the "maven" 3.3.3-jdk-8 image. This pipeline will work on the physical node, but an error will be reported when running in the container.

Docker running in Docker

Running the master or slave node of Jenkins in the container, some people may think that I need privileged mode to use "Docker in Docker", but I don't. J é r ô me Petazzoni published an article ["using Docker-in-Docker to build a continuous integration environment?" Https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/), you should refer to this article.

If you're still using wrapdocker scripts, you should ask yourself why, because it's easier to use:

```sh

Docker run-v ${JENKINS_HOME}: / var/jenkins_home\

-v / var/run/docker.sock:/var/run/docker.sock\

-p 8080V 8080-p 50000R 50000\

Index.csphere.cn/microimages/jenkins

`

This command starts Jenkins and has all the container manipulation functions, so there is no need for privileged mode to start the container, nor does it need "Docker-in-Docker" mode.

There is one thing to note: you cannot use the official Jenkins image here, because jenkins users need to belong to the docker user group in order to use socket, so that docker can be called in the Jenkins in the container, and finally other containers can be built and run through Jenkins.

Jenkins JNLP proxy container

On the "system Management" = > "manage nodes" page, click "New Node" to add slave:

The startup mode of the Jenkins slave node is similar to that of the master node. It also needs to connect to the socket interface of docker. You can start it like this:

```sh

Docker run-v ${JENKINS_HOME}: / var/jenkins_home\

-v / var/run/docker.sock:/var/run/docker.sock\

-- name=jenkins-slave\

-d index.csphere.cn/microimages/jenkins-slave\

-url http://jenkins-master:8080/\

A0a1b92971030d5f5dd69bd972c6cd899f705ddd3699ca3c5e92f937d860be7e\

Test-agent

`

Like the Jenkins master node, you need to make sure that the jenkins user has access to the docker socket interface. I use the Jenkins jnlp slave node container so that the slave container can perform build operations. Note that the secret parameters need to be checked from a slave on the master.

Ready to start building

It is not difficult to start a build process in the container. The problem is that you have to bind the proxy container to the path ${JENKINS_HOME}: / var/jenkins_home on a host, and the container being built also needs access to this directory.

```sh

Docker run-t-d-u 1000 1000-w / var/jenkins_home/workspace/uri-templates-in-docker\

-v / var/jenkins_home/workspace/uri-templates-in-docker:/var/jenkins_home/workspace/uri-templates-in-docker:rw\

-e *-e *

-e *-e *

Maven:3.3.3-jdk-8 cat

`

This container mounts the / var/jenkins_home/workspace/uri-templates-in-docker directory on the host to the containerized environment for Maven to use, and sets this path to the current working path, which works fine on the physical machine, but to execute in the container, I need to try to do this:

This obviously does not work, because I have mapped the docker socket port to the Jenkins proxy container, and all volumes mounted to the Jenkins agent container actually refer to the path on the host. Assuming that ${JENKINS_HOME} on the host is / opt/jenkins_home, the following command should take effect:

```sh

Docker run-t-d-u 1000 1000-w / opt/jenkins_home/workspace/uri-templates-in-docker\

-v / opt/jenkins_home:/var/jenkins_home/workspace/uri-templates-in-docker:rw\

-e *

Maven:3.3.3-jdk-8 cat

`

Summary

Containment of the build environment is a very good idea, which saves a lot of time.

Note that this code may not exactly meet your needs, but at least it's a demo, and I hope this article will help more people use Jenkins containers to build applications.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report