ubuntu中实现docker搭建jenkins环境

做项目集成使用 Jenkins 工具,环境搭建一些记录。

准备环境

  1. Ubuntu系统(Ubuntu 18.04 LTS)
  2. Docker(v.20.10.12)
  3. Jenkins(v.2.319.2)

新创建的云服务器 Ubuntu 系统远程登录 ssh 默认是关闭的,需要在 /etc/ssh/sshd_config 文件中增加 PermitRootLogin yes

Ubuntu 的软件源配置文件是 /etc/apt/sources.list。将系统自带的该文件做个备份,将该文件替换为下面内容,即可使用 NJU软件源镜像。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirror.nju.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirror.nju.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirror.nju.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirror.nju.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirror.nju.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirror.nju.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirror.nju.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirror.nju.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirror.nju.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirror.nju.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

再输入命令更新源,最后输入命令更新一下软件即可。

1
2
sudo apt-get update
sudo apt-get upgrade

安装Docker

如果你过去安装过 docker,先删掉:

1
sudo apt-get remove docker docker-engine docker.io

首先安装依赖:

1
sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

信任 Docker 的 GPG 公钥:

1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

注意这一步多半会报错 timeout,解决方案:

1)先把官方GPG key下载到本地,是一个gpg文件:https://download.docker.com/linux/ubuntu/gpg

2)下载到本地后,采用如下命令,安装官方GPG key:sudo apt-key add /path/gpg(gpg文件路径)

添加软件仓库:

1
2
3
sudo add-apt-repository \
"deb [arch=amd64] https://mirror.nju.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable"

更新 apt 软件包缓存,并安装 docker-ce:

1
2
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

镜像加速:

在配置文件 /etc/docker/daemon.json 中加入如下内容(如果文件不存在请新建该文件):

1
2
3
{
"registry-mirrors": ["https://docker.nju.edu.cn/"]
}

重新启动docker:

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

测试 Docker 是否安装正确:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ docker run --rm hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:507ecde44b8eb741278274653120c2bf793b174c06ff4eaa672b713b3263477b
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

安装Jenkins

Jenkins 官方文档建议使用的 Docker 镜像是 jenkinsci/blueocean image,该镜像包含当前的长期支持 (LTS) 的 Jenkins 版本 (可以投入使用) ,捆绑了所有Blue Ocean插件和功能。这意味着你不需要单独安装Blue Ocean插件。

这里我使用的是 jenkinsci/blueocean 镜像,docker pull命令拉取:

1
docker pull jenkinsci/blueocean

查看本地镜像仓库:

1
2
3
4
5
$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
jenkinsci/blueocean latest 71d1686eed44 23 hours ago 582MB
hello-world latest feb5d9fea6a5 4 months ago 13.3kB

启动 Jenkins 容器应用:

1
2
3
4
5
6
7
8
9
 docker run \
--name jenkins-blueocean \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins-data:/var/jenkins_home \
jenkinsci/blueocean

1c095de1519c16a1bfcf9a4fd6077e2099c1547a4f8cd68565746306279f57ab

配置Jenkins

以上如果一切正常,现在就可以通过 http://<ip-address>:8080 访问 Jenkins 的 Web 界面了。

先进入到容器,再去查看对应的文件 initialAdminPassword 内容:

1
2
3
4
5
$ docker exec -it jenkins-blueocean /bin/bash

bash-5.1$ cat /var/jenkins_home/secrets/initialAdminPassword

b6f6d08fcc474178833001d1fc79be62
文章作者: gzwangu
文章链接: https://gzwangu.github.io/2022/02/07/ubuntu中实现docker搭建jenkins环境/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Clousbin の Blog
支付宝打赏
微信打赏