当前位置: 代码迷 >> 综合 >> How to manage jenkins based on docker
  详细解决方案

How to manage jenkins based on docker

热度:27   发布时间:2023-12-14 23:51:19.0

文章目录

  • Installation
  • Configuration

??今天我们来着重介绍一下如何基于docker环境来安装jenkins。

Installation

[root@xtwj69 ~]# mkdir -p /data/jenkins
[root@xtwj69 ~]# ll /data/jenkins
total 0
drwxr-xr-x. 2 root root  6 Dec 16 10:50 .
drwxr-xr-x. 3 root root 21 Dec 16 10:50 ..
[root@xtwj69 ~]# docker run -d --restart always --name jenkins -p 8080:8080 -p 50000:50000 -u root  -v /data/jenkins:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkinsci/blueocean:1.21.0
922c61f710d435762b912880125fcdbf0c0f4ce87da053b367a3ba13f95a943c
[root@xtwj69 ~]# docker ps -a
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                              NAMES
922c61f710d4        jenkinsci/blueocean:1.21.0   "/sbin/tini -- /usr/…"   31 seconds ago      Up 29 seconds       0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins
[root@xtwj69 ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@xtwj69 ~]# firewall-cmd --zone=public --add-port=50000/tcp --permanent
success
[root@xtwj69 ~]# firewall-cmd --reload
success
[root@xtwj69 ~]# firewall-cmd --zone=public --list-all
public (active)target: defaulticmp-block-inversion: nointerfaces: eth0sources: services: dhcpv6-client sshports: 8080/tcp 50000/tcpprotocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: [root@xtwj69 ~]#

Configuration

打开浏览器,输入http://192.168.3.69:8080/即可打开jenkins初始化页面。

Please wait while Jenkins is getting ready to work …
Your browser will reload automatically when Jenkins is ready.
解法方法: 进入宿主机/data/jenkins/目录,编辑文件hudson.model.UpdateCenter.xml,

[root@xtwj69 ~]# ll /data/jenkins/
total 72
drwxr-xr-x.  15 root root  4096 Dec 16 14:24 .
drwxr-xr-x.   3 root root    21 Dec 16 10:50 ..
drwxr-xr-x.   3 root root    17 Dec 16 14:17 .cache
-rw-r--r--.   1 root root  1666 Dec 16 14:24 config.xml
-rw-r--r--.   1 root root  4025 Dec 16 14:21 copy_reference_file.log
drwxr-xr-x.   3 root root    20 Dec 16 14:17 .groovy
-rw-r--r--.   1 root root   156 Dec 16 14:21 hudson.model.UpdateCenter.xml
-rw-r--r--.   1 root root   370 Dec 16 14:17 hudson.plugins.git.GitTool.xml
-rw-------.   1 root root  1712 Dec 16 14:17 identity.key.enc
drwxr-xr-x.   3 root root    19 Dec 16 14:16 .java
-rw-r--r--.   1 root root  1179 Dec 16 14:27 jenkins.install.InstallUtil.installingPlugins
-rw-r--r--.   1 root root     7 Dec 16 14:17 jenkins.install.UpgradeWizard.state
-rw-r--r--.   1 root root   171 Dec 16 14:17 jenkins.telemetry.Correlator.xml
drwxr-xr-x.   2 root root     6 Dec 16 14:16 jobs
-rw-r--r--.   1 root root     0 Dec 16 14:22 .lastStarted
drwxr-xr-x.   4 root root    37 Dec 16 14:17 logs
-rw-r--r--.   1 root root   907 Dec 16 14:21 nodeMonitors.xml
drwxr-xr-x.   2 root root     6 Dec 16 14:17 nodes
drwxr-xr-x. 101 root root 16384 Dec 16 14:26 plugins
-rw-r--r--.   1 root root   129 Dec 16 14:21 queue.xml.bak
-rw-r--r--.   1 root root    64 Dec 16 14:16 secret.key
-rw-r--r--.   1 root root     0 Dec 16 14:16 secret.key.not-so-secret
drwx------.   4 root root   265 Dec 16 14:17 secrets
drwxr-xr-x.   2 root root   100 Dec 16 14:18 updates
drwxr-xr-x.   2 root root    24 Dec 16 14:17 userContent
drwxr-xr-x.   3 root root    56 Dec 16 14:17 users
drwxr-xr-x.  11 root root  4096 Dec 16 14:16 war
drwxr-xr-x.   2 root root     6 Dec 16 14:17 workflow-libs
[root@xtwj69 ~]# cat /data/jenkins/hudson.model.UpdateCenter.xml 
<?xml version='1.1' encoding='UTF-8'?>
<sites><site><id>default</id><url>https://updates.jenkins.io/update-center.json</url></site>
</sites>
[root@xtwj69 ~]# 

http://updates.jenkins-ci.org/update-center.json 修改成 http://mirror.xmission.com/jenkins/updates/update-center.json,然后重启docker容器。

在这里插入图片描述

这个时候,系统提示输入密码。我们需要进入容器中,将密码找出来。

[root@xtwj69 ~]# docker exec -it jenkins /bin/bash
root@6e5ca7b41e5c:/# ls -la /var/jenkins_home/secrets/initialAdminPassword
-rw-r-----. 1 root root 33 Dec 16 03:05 /var/jenkins_home/secrets/initialAdminPassword
root@6e5ca7b41e5c:/# cat /var/jenkins_home/secrets/initialAdminPassword
314ec2c188db456cabc0ec96878ebf89
root@6e5ca7b41e5c:/# exit
exit
[root@xtwj69 ~]#

我们看到,密码是314ec2c188db456cabc0ec96878ebf89,在浏览器页面中输入该密码即可进行后续配置。

在这里插入图片描述

[root@xtwj69 jenkins]# docker exec -it jenkins /bin/bash 
bash-4.4# mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /var/jenkins_home/apache-maven-3.6.3
Java version: 1.8.0_212, vendor: IcedTea, runtime: /usr/lib/jvm/java-1.8-openjdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.4.3.el7.x86_64", arch: "amd64", family: "unix"
bash-4.4#
bash-4.4# cat .bashrc 
export M2_HOME=/var/jenkins_home/apache-maven-3.6.3
export PATH=$PATH:$M2_HOME/bin
bash-4.4#

参考文献

  • 安装Jenkins
  • JenKins项目自动化构建部署
  相关解决方案