第十一章 Dockerfile安裝Jenkins-2.249.3-1.1

年少縱馬且長歌發表於2021-10-19

一、安裝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 ...";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安裝完畢。

相關文章