一 基础环境准备
[root@node6 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@node6 ~]# yum -y install epel-release
[root@node6 ~]# yum update
[root@node6 ~]# uname -r
3.10.0-957.27.2.el7.x86_64
二 安装docker
2.1 使用脚本安装
[root@node6 ~]# curl -fsSL https://get.docker.com/ | sh
# Executing docker install script, commit: f45d7c11389849ff46a6b4d94e0dd1ffebca32c1 + sh -c 'yum install -y -q yum-utils' + sh -c 'yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo' Loaded plugins: fastestmirror adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo + '[' stable '!=' stable ']' + sh -c 'yum makecache' Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile epel/x86_64/metalink | 6.0 kB 00:00:00 * base: mirror.pregi.net * epel: mirror.pregi.net * extras: mirror.pregi.net * updates: mirror.pregi.net base | 3.6 kB 00:00:00 docker-ce-stable Metadata Cache Created + '[' -n '' ']' + sh -c 'yum install -y -q docker-ce'
2.2 启动docker
[root@node6 ~]# systemctl start docker
[root@node6 ~]# systemctl status docker
2.3 尝试运行jnekins容器
运行jenkins容器
[root@node6 ~]# docker run -d --name jenkins_dev -p 8000:8080 -p 50000:50000 jenkins/jenkins:lts
Unable to find image 'jenkins/jenkins:lts' locally lts: Pulling from jenkins/jenkins 9a0b0ce99936: Pull complete db3b6004c61a: Pull complete f8f075920295: Pull complete 6ef14aff1139: Pull complete 962785d3b7f9: Pull complete 631589572f9b: Pull complete c55a0c6f4c7b: Pull complete 911269a281e7: Pull complete 45099f90ad07: Pull complete bd655133fb06: Pull complete ea116f49f692: Pull complete a9e13a874162: Pull complete bdbca454a0c4: Pull complete 9b2fec49ceaa: Pull complete 765aaab2ff5b: Pull complete a46bfc77dc0f: Pull complete 2cd8e1655f4a: Pull complete 4d138a7aea1a: Pull complete Digest: sha256:c3b6fa9216336022067be94adb9d4696459903bf3e67e01a8d0009994f5ab78d Status: Downloaded newer image for jenkins/jenkins:lts d63c603216f7d24f0662318e754bf44c3ab25d11f5b3dc8ca764818d851d85b6
[root@node6 ~]# ip route
default via 192.168.132.2 dev ens33 default via 192.168.132.2 dev ens33 proto dhcp metric 100 169.254.0.0/16 dev ens33 scope link metric 1002 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 192.168.132.0/24 dev ens33 proto kernel scope link src 192.168.132.136 metric 100 192.168.132.0/24 dev ens33 proto kernel scope link src 192.168.132.147 metric 100
[root@node6 ~]# netstat -ntlp
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1324/master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 25594/sshd tcp6 0 0 ::1:25 :::* LISTEN 1324/master tcp6 0 0 :::8000 :::* LISTEN 56998/docker-proxy tcp6 0 0 127.0.0.1:8005 :::* LISTEN 6297/java tcp6 0 0 :::8009 :::* LISTEN 6297/java tcp6 0 0 :::50000 :::* LISTEN 56984/docker-proxy tcp6 0 0 :::8080 :::* LISTEN 6297/java tcp6 0 0 :::22 :::* LISTEN 25594/sshd
访问50000端口
[root@node6 bin]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d63c603216f7 jenkins/jenkins:lts "/sbin/tini -- /usr/…" 16 minutes ago Up 16 minutes 0.0.0.0:50000->50000/tcp, 0.0.0.0:8000->8080/tcp jenkins_dev
[root@node6 ~]# docker exec -it jenkins_dev \
cat /var/jenkins_home/secrets/initialAdminPassword
c549879f412f4063a5098a45276b0320
或者进入到容器的内部,执行docker exec命令。
sudo docker exec -it jenkins_dev bash
进入到容器内部后,执行下面的命令去获取内容。
cat /var/jenkins_home/secrets/initialAdminPassword
运行jenkins容器,容器使用数据卷
创建jenkins容器,没有使jenkins_home目录中的数据保持持久,简单的说,就是如果删除了容器,那么jenkins_home目录中的数据也会被删除。
因此通过data volumes能保持数据的持久,数据卷在特别的目录,能保持数据持久而不受容器生命周期的影响。 就是在删除容器时,还能保持数据。数据完全和容器能够独立。
三 容器使用数据卷
使用下面命令运行Jenkins容器。
3.1 删除容器
[root@node6 bin]# docker stop d63c603216f7 d63c603216f7 [root@node6 bin]# docker rm d63c603216f7 d63c603216f7
[root@node6 bin]# docker run -d --name jenkins_prod -p 8080:8080 -p 50000:50000 -v jenkins-home-prod:/var/jenkins_home jenkins/jenkins:lts
a53cb2d416f6e3aa1cbb48d81f2e60f254063775436fb95cc273da602ebb4e46
[root@node6 bin]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a53cb2d416f6 jenkins/jenkins:lts "/sbin/tini -- /usr/…" 17 seconds ago Up 16 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins_prod
-v jenkins-home-prod:/var/jenkins_home选项创建一个名为jenkins-home-prod的卷,并和容器内部的/var/jenkins_home目录做映射。
执行下面的命令去查看jenkins_prod容器中的/var/jenkins_home目录内容。
[root@node6 bin]# docker exec -it jenkins_prod ls -lrt /var/jenkins_home
-rw-rw-r-- 1 root root 7152 Oct 28 20:47 tini_pub.gpg -rw-r--r-- 1 jenkins jenkins 50 Nov 4 13:56 copy_reference_file.log drwxr-xr-x 11 jenkins jenkins 4096 Nov 4 13:56 war -rw-r--r-- 1 jenkins jenkins 29 Nov 4 13:56 failed-boot-attempts.txt -rw-r--r-- 1 jenkins jenkins 64 Nov 4 13:56 secret.key -rw-r--r-- 1 jenkins jenkins 0 Nov 4 13:56 secret.key.not-so-secret drwxr-xr-x 2 jenkins jenkins 6 Nov 4 13:56 plugins drwxr-xr-x 2 jenkins jenkins 6 Nov 4 13:57 jobs drwxr-xr-x 2 jenkins jenkins 6 Nov 4 13:57 nodes -rw-r--r-- 1 jenkins jenkins 156 Nov 4 13:57 hudson.model.UpdateCenter.xml -rw------- 1 jenkins jenkins 1712 Nov 4 13:57 identity.key.enc -rw-r--r-- 1 jenkins jenkins 171 Nov 4 13:57 jenkins.telemetry.Correlator.xml drwxr-xr-x 2 jenkins jenkins 24 Nov 4 13:57 userContent drwxr-xr-x 3 jenkins jenkins 19 Nov 4 13:57 logs -rw-r--r-- 1 jenkins jenkins 907 Nov 4 13:57 nodeMonitors.xml -rw-r--r-- 1 jenkins jenkins 7 Nov 4 13:57 jenkins.install.UpgradeWizard.state drwxr-xr-x 3 jenkins jenkins 56 Nov 4 13:57 users drwx------ 4 jenkins jenkins 265 Nov 4 13:57 secrets -rw-r--r-- 1 jenkins jenkins 1658 Nov 4 13:57 config.xml
3.2 列出数据卷
[root@node6 bin]# docker volume ls
DRIVER VOLUME NAME local fffcf955473d4cd319aa9fc6c32a5f32ca09606ff4d4c74d1a5462c67ea4f025 local jenkins-home-prod
3.3 测试数据卷
- 执行以下步骤测试数据卷
- 在jenkins server中做一些改变,修改/var/jenkins_home目录中的内容
- 删除jenkins容器
- 使用下面的命令检查活动的jenkins容器
[root@node6 bin]# docker ps --format "{{.ID}}: {{.Image}} {{.Names}}"
a53cb2d416f6: jenkins/jenkins:lts jenkins_prod
3.4 访问测试
使用http://IP:8080访问jenkins服务器
http://192.168.132.136:8080/login?from=%2F
使用下面的命令获取initialAdminPassword文件的内容
[root@node6 bin]# docker exec -it jenkins_prod \
> cat /var/jenkins_home/secrets/initialAdminPassword
6a8fff13ae8e47ddadef1105de1230e7
获取密码后,粘贴到里面后,安装插件,然后创建First Admin User.
创建用户
创建一个任务
3.5 查看目录
执行下面的命令获取/var/jenkins_home/users目录中的内容
[root@node6 bin]# docker exec -it jenkins_prod ls -lrt
drwxr-xr-x 2 root root 6 Sep 8 10:51 home drwxr-xr-x 2 root root 6 Sep 8 10:51 boot drwxr-xr-x 1 root root 19 Oct 14 00:00 usr drwxr-xr-x 2 root root 6 Oct 14 00:00 srv drwxr-xr-x 3 root root 30 Oct 14 00:00 run drwxr-xr-x 2 root root 6 Oct 14 00:00 opt drwxr-xr-x 2 root root 6 Oct 14 00:00 mnt drwxr-xr-x 2 root root 6 Oct 14 00:00 media drwxr-xr-x 2 root root 34 Oct 14 00:00 lib64 drwxr-xr-x 1 root root 30 Oct 14 00:00 lib drwx------ 1 root root 24 Oct 18 23:40 root drwxr-xr-x 1 root root 26 Oct 28 20:47 var drwxr-xr-x 1 root root 18 Oct 28 20:47 sbin drwxr-xr-x 1 root root 18 Oct 28 20:48 bin drwxr-xr-x 1 root root 66 Nov 4 13:56 etc dr-xr-xr-x 147 root root 0 Nov 4 13:56 proc dr-xr-xr-x 13 root root 0 Nov 4 13:56 sys drwxr-xr-x 5 root root 340 Nov 4 13:56 dev drwxrwxrwt 1 root root 130 Nov 4 13:56 tmp
3.6 删除容器
[root@node6 bin]# docker kill jenkins_prod jenkins_prod [root@node6 bin]# docker rm jenkins_prod jenkins_prod [root@node6 bin]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.7 查看已存在容器以及卷
[root@node6 bin]# docker ps -a --format "{{.ID}}: {{.Image}} {{.Names}}" [root@node6 bin]# docker volume ls DRIVER VOLUME NAME local fffcf955473d4cd319aa9fc6c32a5f32ca09606ff4d4c74d1a5462c67ea4f025 local jenkins-home-prod
3.8 创建名为jenkins_prod的容器,并使用已经存在的jenkins-home-prod卷。
[root@node6 bin]# docker run -d --name jenkins_prod -p 8080:8080 \ > -p 50000:50000 -v jenkins-home-prod:/var/jenkins_home \ > jenkins/jenkins:lts b119e7f28b507b3a3e9dd4a91a5d361bbadbfdad35d4cf95cc467bc6daed8d3a
3.9 访问测试
访问 http://ip:8080,可以直接看到登陆界面,而不是再次让我们初始登陆
直接出现登陆界面
登陆进入后,数据依然在
实验完成
参考文档:藏雪原老师视频教程https://edu.csdn.net/course/play/9051/187998