docker-compose安装JenKins

一、Jenkins

Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。

JenKins官网

二、安装JenKins的前提条件

硬件 最低要求 推荐
内存 256M 1GB或更高
硬盘 1GB(作为一个Docker容器运行jenkins的话推荐10GB) 50GB或更高

三、部署规划

服务器IP 192.168.113.48
端口 8928
安装目录 /home/work/docker-jenkins
数据卷映射目录 /home/work/docker-jenkins/data

四、安装JenKins

  • 在安装目录下创建docker-jenkins目录,创建成功后进入该目录

    mkdir docker-jenkins
    cd docker-jenkins
    
  • 新建并按照规划编辑docker-compose.yml文件,编辑完后保存退出

    vim docker-compose.yml
    
    version: '3'
    services:
      jenkins:
        image: 'jenkins/jenkins:lts'
        container_name: jenkins
        restart: always
        environment:
          - TZ=Asia/Shanghai
        ports:
          - '8928:8080'
          - '50000:50000'
        volumes:
          - '/etc/localtime:/etc/localtime'
          - './data:/var/jenkins_home'
          - '/var/run/docker.sock:/var/run/docker.sock'
          - '/usr/bin/docker:/usr/bin/docker'
    
  • 在安装目录下执行相关命令,启动容器

    docker-compose up -d
    

    执行命令后发现服务并没有运行,查看日志:

    [root@node03 docker-jenkins]# docker-compose ps
     Name                Command                 State      Ports
    -------------------------------------------------------------
    jenkins   /sbin/tini -- /usr/local/b ...   Restarting        
    [root@node03 docker-jenkins]# docker-compose logs -f
    Attaching to jenkins
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    jenkins    | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    

    通过日志发现,是由于权限不足导致服务没有正常运行, 因为当映射本地数据卷时,/home/work/docker-jenkins目录的拥有者为root用户,而容器中jenkins user的uid为1000;执行以下命令即可。

    chown -R 1000:1000 /home/work/docker-jenkins
    

    执行命令后会发现服务已正常启动:

    [root@node03 docker-jenkins]# chown -R 1000:1000 /home/work/docker-jenkins
    You have new mail in /var/spool/mail/root
    [root@node03 docker-jenkins]# docker-compose ps
     Name                Command               State                        Ports                      
    ---------------------------------------------------------------------------------------------------
    jenkins   /sbin/tini -- /usr/local/b ...   Up      0.0.0.0:50000->50000/tcp, 0.0.0.0:8928->8080/tcp
    

五、验证Jenkins

  • 在浏览器输入ip:端口,页面提示解锁Jenkins

    BAXFoj.png

    根据页面提示信息,密码路径(路径为容器中路径)如上图所示,在此之前已经做了数据映射,不用登录容器,直接在宿主机查看即可:

    [root@node03 docker-jenkins]# cat /home/work/docker-jenkins/data/secrets/initialAdminPassword 
    d1d17726052b4716acd6315a276fd0fb
    

    将查出来的密码复制到浏览器中即可。点击"继续"就会出现以下界面:

    BAj1u8.png

    根据自己所需进行选择即可。(建议选择“安装推荐的插件”,若插件安装失败,多试几次即可)

插件下载较慢是由于服务器在国外,所以下载缓慢,可以更换为国内插件源:
Jenkins更换国内插件镜像源教程

posted @ 2020-11-05 16:25  林安杰  阅读(2616)  评论(0编辑  收藏  举报