Jenkins:通过Docker安装Jenkins

通过本机Docker构建项目

环境准备

  • 安装docker环境

安装Jenkins

执行以下命令,注意-u root是必须的

mkdir -p /DockerVolume/jenkinsHome
docker run \
  -u root \
  --name jenkins-blueocean \
  -d \
  -p 8080:8080 \
  -p 50000:50000 \
  -v /DockerVolume/jenkinsHome:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /etc/localtime:/etc/localtime \ 
  jenkinsci/blueocean 

坑:这种方法在jenkins中配置『docker run ……』时,参数-v path:docerPath中的path指得是宿主机的path,而执行在jenkins执行${pwd}则是取docker容器的dockerPath,此时会报错:找不到项目文件。
我解决坑的办法:

// 在Jenkinsfile中,进行volume映射时,替换路径:-v $(pwd | sed  "s#/var/jenkins_home#/DockerVolume/jenkinsHome#g"):/usr/src/mymaven
 sh 'docker run --name ${containerName} --restart=on-failure:5 \
                     -p ${masterServerPort}:${springServerPort} -d -v maven-repo:/usr/share/maven/ref \
                     -v $(pwd | sed  "s#/var/jenkins_home#/DockerVolume/jenkinsHome#g"):/usr/src/mymaven \
                     -w /usr/src/mymaven ${imageName} sh -c "mvn clean install -DskipTests && java -jar target/healthy-1.0.0.jar"'

如果上面的Docker镜像不好,尝试使用下以下的:

docker run \
  -u root \
  --name jenkins-blueocean \
  -d \
  -p 8080:8080 \
  -p 50000:50000 \
  -v /DockerVolume/jenkinsHome:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /etc/localtime:/etc/localtime \
  jenkins/jenkins

通过Docker in Docker构建项目

前言

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

环境准备

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

安装jenkins

  1. 创建 bridge network

    docker network create jenkins
    
  2. 在 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 
    
posted @ 2021-08-25 13:50  Feng1024  阅读(329)  评论(0编辑  收藏  举报