第九章 Dockerfile安装Jenkins-2.249.3-1.1

一、安装Docker

Docker部署Jenkins前提已经安装Docker,这边脚本安装Docker。
#1.编写Docker安装脚本
[root@ip-10-0-12-212 ~]# vim Ins_Docker.sh 
#!/usr/bin/bash
# Author:jh
# Time:2021-10-09  11:54:22
# Name:Ins_Docker.sh
# Version: 1.0
# Discription: To  
source /etc/init.d/functions

rely_software=(yum-utils device-mapper-persistent-data lvm2)

echo "-----------------#1.检测系统内核版本-----------------"
kernel_version=`uname -a |awk '{print $3}'|cut -d. -f1`
[ $kernel_version -ge 3 ] &&  action "The System Install Docker is ..." /usr/bin/true || action "The System Can not Install Docker is ..." /usr/bin/false;exit
 
echo "-----------------#2.安装Docker依赖软件-----------------"
for i in ${rely_software[*]}
do       
    rpm -q $i &>/dev/null
    if [ $? -eq 0 ];then
        echo "$i is installed"            
    else            
        yum -y install $i  &>/dev/null                                                           
        action "$i is installing"  /usr/bin/true           
    fi                     
done

echo "-----------------#3.添加Docker镜像源-----------------"
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[ $? -eq 0 ] && action "Docker Image add is ..." /usr/bin/true || echo "Docker Image add is ..." /usr/bin/false;exit

echo "-----------------#4.安装Docker-----------------"
yum -y install docker-ce 
rpm -qa |grep docker-ce 
[ $? -eq 0 ] && action "Docker-ce is installed ..." /usr/bin/true || action "Docker-ce is install ..." /usr/bin/false;exit

echo "-----------------#5.启动Docker并设置开机自启-----------------"
systemctl enable --now docker
[ $? -eq 0 ] && action "Docker Start is ...;" /usr/bin/true || action "Docker start is ..." /usr/bin/false
docker_version=`docker -v |awk -F ',' '{print $1}'`

echo "-----------------#6.设置Docker加速器-----------------"
sudo mkdir -p /etc/docker
sudo cat >/etc/docker/daemon.json <<EOF
{
      "registry-mirrors": ["https://8mh75mhz.mirror.aliyuncs.com"]
  }
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

echo "-----------------#7.查看Docker是否安装完成-----------------"
cat << EOF
+-------------------------------------------------+
| $docker_version 安 装 已 完 成           |
| 请 使 用 相 关 服 务 !                          |
+-------------------------------------------------+
EOF

echo "-----------------#8.迁移Docker根目录-----------------"
systemctl  stop docker
mv /var/lib/docker/ /data/
sed -i 's#ExecStart=/usr/bin/dockerd#ExecStart=/usr/bin/dockerd --graph=/data/docker#g' /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl start docker.service

echo "-----------------#9.验证Docker根目录是否生效-----------------"
docker info |grep "Docker Root Dir"

sleep 5

echo ""-----------------你的小可爱已完成Docker全部部署-----------------"
#2.给脚本添加执行权限
[root@ip-10-0-12-212 ~]# chmod +x Ins_Docker.sh 

#3.执行脚本
[root@ip-10-0-12-212 ~]# sh Ins_Docker.sh 

二、编写Dockerfile

#1.创建Dockerfile存放目录
[root@ip-10-0-12-212 ~]# mkdir jenkins
[root@ip-10-0-12-212 ~]# cd jenkins

#2.编写Dockerfile
[root@ip-10-0-12-212 jenkins]# vim Dockerfile 
#Is my first jenkins Dockerfile
# Version 1.0

# Base images 基础镜像
FROM centos:7

#MAINTAINER 维护者信息
MAINTAINER Jin.Hui

#ADD  文件放在当前目录下,拷过去会自动解压
ADD jdk-8u131-linux-x64.tar.gz /opt/
COPY epel-release-latest-7.noarch.rpm /opt/
COPY jenkins-2.249.3-1.1.noarch.rpm /opt/

#RUN 执行以下命令
RUN rpm -ivh /opt/epel-release-latest-7.noarch.rpm \
    && rpm -ivh /opt/jenkins-2.249.3-1.1.noarch.rpm \
    && ln -s /opt/jdk1.8.0_131 /opt/jdk

COPY jenkins /etc/sysconfig/jenkins

#ENV 设置环境变量
ENV JAVA_HOME /opt/jdk
ENV PATH $JAVA_HOME/bin:$PATH

#VILUME 用于指定持久化目录
VOLUME ["/var/lib/jenkins"]

#EXPOSE 映射端口
EXPOSE 8080
EXPOSE 50000

#CMD 运行以下命令
CMD java -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war
#3.上传相关配置文件
[root@ip-10-0-12-212 jenkins]# rz
[root@ip-10-0-12-212 jenkins]# ll
total 246844
-rw-r--r-- 1 root root       820 Oct 19 15:36 Dockerfile
-rw-r--r-- 1 root root     15608 Oct 19 11:30 epel-release-latest-7.noarch.rpm
-rw-r--r-- 1 root root 185540433 Oct 18 15:25 jdk-8u131-linux-x64.tar.gz
-rw------- 1 root root      3113 Oct 18 17:37 jenkins
-rw-r--r-- 1 root root  67200896 Nov  5  2020 jenkins-2.249.3-1.1.noarch.rpm

三、构建Dockerfile

[root@ip-10-0-12-212 jenkins]# docker build --no-cache -t datalake_jenkins:v1 .
... ...
Successfully built 1e2736247512
Successfully tagged datalake_jenkins:v1

四、运行Docker容器

#1.运行Jenkins
[root@ip-10-0-12-212 jenkins]# docker run -d --name datalake_jenkins -p 10240:8080  -p 10241:50000 datalake_jenkins:v1
517aa3befbca0d9bfc1558c1d4fd4d5b3e0f4bfed6264c22b05254e8b2efb535

#2.查看Docker进程
[root@ip-10-0-12-212 jenkins]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                                                                        NAMES
517aa3befbca   datalake_jenkins:v1   "/bin/sh -c 'java -D…"   8 seconds ago   Up 6 seconds   0.0.0.0:10240->8080/tcp, :::10240->8080/tcp, 0.0.0.0:10241->50000/tcp, :::10241->50000/tcp   datalake_jenkins

五、验证Jenkins

#1.查看持久化存储
[root@ip-10-0-12-212 jenkins]# docker volume ls
DRIVER    VOLUME NAME
local     0cdee244e8f9bb50d0b6bfd34bf2bd0718235bcacca266d74747ae5a9618b2b0

#2.删除没有镜像使用的存储
[root@ip-10-0-12-212 jenkins]# docker volume rm $(docker volume ls -qf dangling=true)

#3.查看持久化存储位置
[root@ip-10-0-12-212 jenkins]# docker inspect 517aa3befbca |grep Source
                "Source": "/data/docker/volumes/0cdee244e8f9bb50d0b6bfd34bf2bd0718235bcacca266d74747ae5a9618b2b0/_data",

#4.查看持久化存储数据
[root@ip-10-0-12-212 jenkins]# cd /data/docker/volumes/0cdee244e8f9bb50d0b6bfd34bf2bd0718235bcacca266d74747ae5a9618b2b0/_data
[root@ip-10-0-12-212 _data]# ll
total 32
-rw-r--r--  1 root root 1640 Oct 19 17:02 config.xml
-rw-r--r--  1 root root  156 Oct 19 17:01 hudson.model.UpdateCenter.xml
-rw-------  1 root root 1712 Oct 19 17:01 identity.key.enc
-rw-r--r--  1 root root    7 Oct 19 17:01 jenkins.install.UpgradeWizard.state
-rw-r--r--  1 root root  171 Oct 19 17:01 jenkins.telemetry.Correlator.xml
drwxr-xr-x  2 root root    6 Oct 19 17:01 jobs
drwxr-xr-x  3 root root   19 Oct 19 17:01 logs
-rw-r--r--  1 root root  907 Oct 19 17:01 nodeMonitors.xml
drwxr-xr-x  2 root root    6 Oct 19 17:01 nodes
drwxr-xr-x  2 root root    6 Oct 19 17:01 plugins
-rw-r--r--  1 root root   64 Oct 19 17:01 secret.key
-rw-r--r--  1 root root    0 Oct 19 17:01 secret.key.not-so-secret
drwx------  4 root root  265 Oct 19 17:01 secrets
drwxr-xr-x  2 root root   67 Oct 19 17:02 updates
drwxr-xr-x  2 root root   24 Oct 19 17:01 userContent
drwxr-xr-x  3 root root   56 Oct 19 17:01 users
drwxr-xr-x 11 root root 4096 Oct 19 17:01 war

#5.WEB页面登录访问
http://52.82.80.128:10240

六、安装Jenkins

#1.解锁Jenkins,找到Jenkins初始密码,输入此密码,点击继续
[root@ip-10-0-12-212 _data]# cat secrets/initialAdminPassword 
5900733b0c184e8f80b0650b8137cc9b

#2.选择安装推荐的插件

#3.填入相应的管理用户信息,点击保存并完成。

#4.配置此处Jenkins的URL。

#5.此处Jenkins已经安装就绪,点击开始使用Jenkins。

#6.此处已经进入Jenkins页面,右下角显示安装的版本号,至此Jenkins安装完毕。

posted @ 2021-10-19 21:56  年少纵马且长歌  阅读(585)  评论(4编辑  收藏  举报