Docker快速部署Jenkins CICD环境

1. 安装docker-compose

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod a+x /usr/local/bin/docker-compose
$ docker-compose version

2. docker-compose安装jenkins

mkdir -p /home/docker-compose_dir/cicd/

install_befor.sh

#!/usr/bin/env sh
[ -d /data/jenkins/ ] || mkdir -p /data/jenkins/


#-------------- jenkins 登录密码 ------------------------------------
#cat /var/jenkins_home/secrets/initialAdminPassword
#57d5a725c24e495084661ba073360824

# 对应
#[root@localhost jenkins]# cat /data/jenkins/secrets/initialAdminPassword
#9fdb4172b14649aea37260a75ae00adf

# 或者
#[root@localhost jenkins]# docker exec docker_id cat /var/jenkins_home/secrets/initialAdminPassword
#9fdb4172b14649aea37260a75ae00adf

docker-compose.yaml

version: '3.1'
services:
  jenkins:
    image: registry.cn-hangzhou.aliyuncs.com/devops_hu/jenkins:1.0.2
    volumes:
      - /data/jenkins/:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker
      - /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
    ports:
      - "8080:8080"
    expose:
      - "8080"
      - "50000"
    privileged: true
    user: root
    restart: always
    container_name: jenkins
    environment:
      JAVA_OPTS: '-Djava.util.logging.config.file=/var/jenkins_home/log.properties'
sh /home/docker-compose_dir/cicd/install_befor.sh
sh /home/docker-compose_dir/cicd/ && docker-compose up -d

3. nginx发布jenkins

3.1 安装nginx

yum install -y nginx
systemctl stop firewalld.service
mkdir -p /data/log/nginx && chown -R nginx:nginx /data/log/nginx
# 关闭selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config

[root@localhost log]# systemctl daemon-reload
[root@localhost log]# systemctl enable nginx
[root@localhost log]# systemctl start nginx

[root@localhost log]# ps aux|grep nginx
root     20903  0.0  0.1 105500  1980 ?        Ss   23:37   0:00 nginx: master process /usr/sbin/nginx
nginx    20904  0.0  0.1 105968  2912 ?        S    23:37   0:00 nginx: worker process
nginx    20905  0.0  0.1 105968  2912 ?        S    23:37   0:00 nginx: worker process
root     20907  0.0  0.0 112808   964 pts/0    S+   23:37   0:00 grep --color=auto nginx

3.2 配置文件

# proxy_pass IP修改为docker宿主机ip
cat > /etc/nginx/conf.d/jenkins.aliyun.com.conf<<-"eof"
server {
 listen 80;
 server_name jenkins.aliyun.com;
 charset utf-8;
 access_log /data/log/nginx/jenkins.aliyun.com.access.log main;
 error_log /data/log/nginx/jenkins.aliyun.com.error.log;
 client_max_body_size 3072m;
 location / {
   index index.html index.htm;
   proxy_pass http://192.168.1.40:8080;
   proxy_set_header           Host $host;
   proxy_set_header           X-Real-IP $remote_addr;
   proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}
eof

3.3 初始化jenkins

# jenkins初始化域名配置

#注意!!:这里一定要配置为域名。
Jenkins URL:http://jenkins.aliyun.com

在hosts文件中写入

192.168.1.xxx    jenkins.aliyun.com

访问Jenkins使用 http://jenkins.aliyun.com

4. jenkins的docker容器里面运行docker

docker命令
如果需要在jenkins里面,执行docker命令,请参考链接:
https://www.cnblogs.com/xiao987334176/p/13470724.html

5. 参考

https://www.cnblogs.com/xiao987334176/p/13373198.html
https://blog.csdn.net/qiuyeyijian/article/details/104507440