概况
最近将 jenkins 迁移到了 docker 里面,可以直接使用官方提供的镜像 jenkins/jenkins,但我想在 jenkins 里面使用 docker,并且在 jenkins 里面构建的镜像能直接保存在 jenkins 的宿主机,在 jenkins 的全局工具配置里面是有 Docker 这个选项的,但是因为某些原因你在国内就是安装不了。
这篇文章 Running Docker in Jenkins(in Docker)写的挺好,提到了两个概念
DooD(Docker-outside-of-Docker) 在 docker 容器里面安装 docker,然后通过
/var/run/docker.sock
与宿主机的 docker 关联,成为宿主机的兄弟容器,在该容器里面执行docker 命令相当于在宿主机里面执行,且可以将镜像安装到宿主机里面DinD (Docker-in-Docker)在 docker 容器里面安装 docker,与宿主机是完全独立的两套 docker 环境
安装
综上,我选择的是 DooD 的方式,在 gitlab 上面搜到了一个 docker-jenkins-dood 的 repo,但是人家是可以在线安装 docker-engine 的,所以针对国情需要修改一下。
首先是需要替换一下 sources.list 文件使用阿里云镜像
[root@localhost docker-jenkins]# cat sources.listdeb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-propertiesdeb http://archive.canonical.com/ubuntu xenial partner deb-src http://archive.canonical.com/ubuntu xenial partner deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
构建 docker 镜像
FROM jenkins/jenkins:lts MAINTAINER javahuang <javahrp@gmail.com># Install necessary packages# USER root COPY sources.list /etc/apt/sources.list RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5 \ && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32 \ && apt-get update \ && apt-get install -y sudo \ && sudo apt-get install -y supervisor iptables libdevmapper1.02.1 libltdl7 libseccomp2 rsync \ && sudo rm -rf /var/lib/apt/lists/*# Install docker-engine# According to Petazzoni's article:# ---------------------------------# "Former versions of this post advised to bind-mount the docker binary from# the host to the container. This is not reliable anymore, because the Docker# Engine is no longer distributed as (almost) static libraries."# ARG docker_version=18.03.1-ce# RUN curl -sSL https://get.docker.com/ | sh && \# apt-get purge -y docker-engine && \# apt-get install docker-engine=${docker_version}-0~jessieCOPY docker-ce_18.03.1_ce-0_debian_amd64.deb /root RUN cd /root \ && dpkg -i docker-ce_18.03.1_ce-0_debian_amd64.deb# Make sure jenkins user has docker privilegesRUN usermod -aG docker jenkins# Install initial plugins# USER jenkins# COPY plugins.txt /usr/share/jenkins/plugins.txt# RUN /usr/local/bin/plugins.sh /usr/share/jenkins/plugins.txt# supervisordUSER root# Create log folder for supervisor and jenkinsRUN mkdir -p /var/log/supervisor RUN mkdir -p /var/log/jenkins# Copy the supervisor.conf file into DockerCOPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf# Start supervisord when running the containerCMD /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
docker build -t docker-jenkins:v1 .
启动 jenkins
docker run --restart=always -d --name jenkins -v /var/run/docker.sock:/var/run/docker.sock -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 docker-jenkins:v1
测试
[root@localhost]# docker exec -it jenkins /bin/bashroot@90f540fa6bd8:/# su - jenkinsjenkins@90f540fa6bd8:~$ docker images# 可以看到返回的是宿主机的镜像...
作者:进击de大黄
链接:https://www.jianshu.com/p/0ecedb072c69
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦