Jenkins:通过Docker安装Jenkins,通过Docker in Docker构建项目

前言

由于以下脚本及命令适配了国内的网络条件,如过是在国外的服务器,可直接参考Jenkins英文官网进行安装

环境准备

  • 安装docker环境
  • 推荐配置
    • 4 GB+ 内存
    • 50 GB+ 磁盘空间

安装jenkins

  1. 创建 bridge network
docker network create jenkins
  1. 在 Jenkins 子节点安装 docker:dind 镜像,目的是能够在子节点中运行Docker命令
docker run \
  --name jenkins-docker \
  --rm \
  --detach \
  --privileged \
  --network jenkins \
  --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume jenkins-docker-certs:/certs/client \
  --volume jenkins-data:/var/jenkins_home \
  --publish 2376:2376 \
  docker:dind \
  --storage-driver overlay2

参数解释

  • --privileged:目前使用 docker:dind 需要privileged权限才能正常使用。较低的Linux内核版本低可能会导致失败

注意:

  • 运行该容器时应该把你想要的端口都暴露出来,比如:443,80端口等
  1. 自定义官方Jenkins镜像,通过
    3.1 把以下内容写入Dockerfile中

    FROM jenkins/jenkins:2.289.3-lts-jdk11
    USER root
    RUN os_release="$(cat /etc/os-release | grep VERSION_CODENAME | awk -F '=' '{print $2}')" &&\
    echo "deb http://mirrors.aliyun.com/debian/ ${os_release} main non-free contrib\n\
    deb-src http://mirrors.aliyun.com/debian/ ${os_release} main non-free contrib\n\
    deb http://mirrors.aliyun.com/debian-security ${os_release}/updates main\n\
    deb-src http://mirrors.aliyun.com/debian-security ${os_release}/updates main\n\
    deb http://mirrors.aliyun.com/debian/ ${os_release}-updates main non-free contrib\n\
    deb-src http://mirrors.aliyun.com/debian/ ${os_release}-updates main non-free contrib\n\
    deb http://mirrors.aliyun.com/debian/ ${os_release}-backports main non-free contrib\n\
    deb-src http://mirrors.aliyun.com/debian/ ${os_release}-backports main non-free contrib\n"\
    > /etc/apt/sources.list
    RUN apt-get update && apt-get install -y apt-transport-https \
    ca-certificates curl gnupg2 \
    software-properties-common
    RUN curl -fsSL  http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | apt-key add -
    RUN apt-key fingerprint 0EBFCD88
    RUN add-apt-repository \
    "deb [arch=amd64]  http://mirrors.aliyun.com/docker-ce/linux/debian \
    $(lsb_release -cs) stable"
    RUN apt-get update && apt-get install -y docker-ce-cli
    USER jenkins
    RUN jenkins-plugin-cli --plugins "blueocean:1.24.7 docker-workflow:1.26"
    

    3.2 通过Dockerfile构建镜像

    docker build -t myjenkins-blueocean:1.1 .
    
  2. 运行Docker

docker run \
  --name jenkins-blueocean \
  --rm \
  --detach \
  --network jenkins \
  --env DOCKER_HOST=tcp://docker:2376 \
  --env DOCKER_CERT_PATH=/certs/client \
  --env DOCKER_TLS_VERIFY=1 \
  --publish 8080:8080 \
  --publish 50000:50000 \
  --volume jenkins-data:/var/jenkins_home \
  --volume jenkins-docker-certs:/certs/client:ro \
  myjenkins-blueocean:1.1 
上一篇:codeup.aliyun.com,vue 流水线配置


下一篇:centos7定时同步时间