docker搭建jenkins环境

环境准备:CentOS7;docker

  1. 安装docker,参照如下教程:
    https://www.cnblogs.com/qgc1995/archive/2018/08/29/9553572.html
  2. docker上搭建Jenkins
    1. 选择lts的jenkins最新版本并拉取
      su
      docker pull jenkins/jenkins:lts
    2. 新建jenkins用户的工作目录并添加权限。ps:文件夹的用户必须1000:1000。博主使用0,出现了异常。
      mkdir /home/jenkins
      
      chown /home/jenkins/ 1000:1000 jenkins
    3. 启动容器
      docker run -itd -p 9090:8080 -p 50000:50000 --name jenkins --privileged=true -v /home/jenkins:/var/jenkins_home jenkins/jenkins:lts
      • --privileged=true 让容器有root权限,方便进入容器操作
      • -p 9090:8080 jenkins的web访问端口9090
      • -v /home/jenkins:/var/jenkins_home 容器/var/jenkins_home路径映射到宿主机/home/jenkins
      准备启动Jenkins
      • 查看本机ip:ifconfig
      • 命令行安装Chrome浏览器(系统内未发现火狐浏览器,有内置浏览器可跳过)
        wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
        
        yum install -y lsb
        
        yum localinstall google-chrome-stable_current_x86_64.rpm

        chrome浏览器快捷方式放到桌面:/usr/share/application/ 目录下Google Chrome图标右键复制到桌面

        cd /usr/share/application/
        
        nautilus .
      • 浏览器地址栏输入http://ip:9090/,如:http://192.168.122.1:9090/

    4. 启动并配置Jenkins
      启动页输入管理员密码,密码地址:/var/jenkins_home/secrets/initialAdminPassword
      ps:容器/var/jenkins_home路径映射到宿主机/home/jenkins下,在宿主机/home/jenkins/secrets/initialAdminPassword地址可以找到密码
      cat /home/jenkins/secrets/initialAdminPassword

      自定义Jenkins界面选择<安装推荐的插件>;自动下载插件

      插件下载完成后,设置admin账户和密码


      点击保存并完成即完成。
    5.  在启动Jenkins过程中出现空白页/插件无法下载参照如下方案解决

      • 修改update-center.json的更新地址
        vi /home/jenkins/hudson.model.UpdateCenter.xml

        将https://updates.jenkins.io/update-center.json替换为http://mirror.xmission.com/jenkins/updates/update-center.json;按esc键:wq保存退出

        <?xml version='1.1' encoding='UTF-8'?>
        <sites>
          <site>
            <id>default</id>
            <url>https://mirror.xmission.com/jenkins/updates/update-center.json</url>
          </site>
        </sites>
      • 替换插件下载源
        cd /home/jenkins/updates
        
        sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json

        浏览器url地址后加上 restart 重启jenkins。如:http://ip:9090/restart

  3. 一些小技巧
    重启centOS系统后,Jenkins浏览器无法打开
    docker ps -a
    docker restart 镜像ID

    物理机无法访问虚拟机docker中Jenkins

    systemctl stop docker
    
    ifconfig docker0 down
    
    brctl delbr docker0
    
    systemctl start docker
    
    ps:物理机访问Jenkins的ip和虚拟机ip不一致


    本文参考自博客:
    https://www.cnblogs.com/qgc1995/archive/2018/08/29/9553572.html
    https://www.cnblogs.com/yoyoketang/p/12115378.html
    https://www.cnblogs.com/hellxz/p/jenkins_install_plugins_faster.html
    https://blog.csdn.net/u012448904/article/details/81051969
    https://blog.csdn.net/FishLearning/article/details/103886939
    https://blog.csdn.net/u012129607/article/details/79616547

posted @ 2020-04-08 23:02  牵猫溜狗  阅读(192)  评论(0编辑  收藏