04. 打造自己的jenkins镜像

一. Dockerfile

From jenkins:2.60.3
MAINTAINER 潘志勇 <sd4015700@126.com>
LABEL Description="org.pzy/jenkins2:1.0"  Version="1.0"
# 切换为超级管理员
USER root
# 添加oracle的jre8
ADD server-jre-8u162-linux-x64.tar.gz  /usr/lib/jvm/
# 修改工作目录
WORKDIR /usr/lib/jvm/
# 删除工作目录下, 所有以 ._ 开头的文件或文件夹
RUN find . -name "._*" | xargs rm -rf
# 改变工作目录下的jre的所有者为jenkins
RUN chown -R jenkins /usr/lib/jvm/jdk1.8.0_162
# 修改时区为东八区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 切换为jenkins用户
USER ${user}

解决问题

  • 在jenkins官方镜像的基础上, 将系统时区修改为东八区

使用镜像创建容器:

docker run -d --net=backend -p 9000:8080 -p 50000:50000 --name jenkins \
-v /Users/pan/docker/container_ext_software/apache-maven-3.5.2:/usr/lib/maven \
-v /Users/pan/docker/volume/jenkins:/var/jenkins_home \
-v /Users/pan/maven_repo:/var/maven_repo org.pzy/jenkins2:1.0

参数说明:

-d: 后台运行
--net=backend: 将容器加入backend网络
-p 9000:8080: 将宿主机的9000端口与容器的8080端口绑定
-p 50000:50000: 将宿主机的9000端口与容器的8080端口绑定 
-v /Users/pan/docker/container_ext_software/apache-maven-3.5.2:/usr/lib/maven  将宿主机的maven,挂载到容器的/usr/lib/maven路径
-v /Users/pan/docker/volume/jenkins:/var/jenkins_home 将宿主机的jenkins目录挂载到容器的/var/jenkins_home
-v /Users/pan/maven_repo:/var/maven_repo org.pzy/jenkins2:1.0 将宿主机的maven本地仓库,挂载到容器的/var/maven_repo目录

二. 打造自己的启动脚本

该启动脚本会自动创建backend网络, 并将当前启动的容器加入到backend网络中.

# 重复执行start命令并不会重复启动
echo "jenkins容器操作脚本,用于容器的启动,停止,删除以及状态查看!"
echo "入参:"$1


containerId=`docker ps -a | grep "jenkins" | grep "Up" | awk '{print $1}'`
if [ "$1" = "start" ]; then

  containerId=`docker network ls | grep backend | awk '{print $1}'`
  if [ -z "$containerId" ]; then
    # 说明backend网络不存在,创建backend网络
    echo "自动创建backend网络!"
    docker network create backend
  fi
  containerId=`docker ps -a | grep "jenkins" | grep "Up" | awk '{print $1}'`

  if [ -z "$containerId" ]; then
    # 容器id为空,说明jenkins未启动,或已停止
    echo "容器jenkins状态: "`docker ps -a | grep "jenkins"`

    containerId=`docker ps -a | grep "jenkins" | grep "Exited" | awk '{print $1}'`
    if [ -n "$containerId" ]; then
      # 容器id非空, 说明容器已停止
      echo "重启容器: jenkins"
      docker start jenkins
    else
      # 容器id不为空, 说明容器未启动
      echo "启动容器: jenkins"
      docker run -d --net=backend -p 9000:8080 -p 50000:50000 --name jenkins -v /Users/pan/docker/container_ext_software/apache-maven-3.5.2:/usr/lib/maven -v /Users/pan/docker/volume/jenkins:/var/jenkins_home -v /Users/pan/maven_repo:/var/maven_repo org.pzy/jenkins2:1.0
    fi
  fi

  containerId=`docker ps -a | grep "jenkins" | grep "Up" | awk '{print $1}'`
  if [ -n "$containerId" ]; then
    echo "容器jenkins已启动,并已加入backend网络!"
    echo "使用[/Users/pan/docker/volume/jenkins/data]作为数据目录!"
    echo "打开浏览器: http://localhost:8000 即可访问!"
  else
    echo "容器jenkins启动异常! 请使用[docker logs]命令容器名称查看日志"
  fi
  echo "容器jenkins状态: "`docker ps -a | grep "jenkins"`

elif [ "$1" = "stop" ]; then
  if [ -n "$containerId" ]; then
    # 容器哦id非空, 表示容器已启动,执行容器停止命令
    echo "停止jenkins!"
    docker stop jenkins
    containerId=`docker ps -a | grep "jenkins" | grep "Exited" | awk '{print $1}'`
    if [ -z "$containerId" ]; then
      # 容器id为空, 表示容器未成功停止
      echo "容器jenkins停止失败! 请使用[docker logs]命令查看具体日志!"
    else
      echo "容器jenkins已停止!"
    fi
    echo "容器jenkins状态: "`docker ps -a | grep "jenkins"`
  else
    echo "容器jenkins已停止!"
    echo "容器jenkins状态: "`docker ps -a | grep "jenkins"`
  fi
elif [ "$1" = "status" ]; then
  if [ -z "$containerId" ]; then
    echo "容器jenkins未启动!"
  else
    echo "容器jenkins已启动!"
  fi
  echo "容器jenkins状态: "`docker ps -a | grep "jenkins"`
elif [ "$1" = "remove" ]; then
  containerId=`docker ps -a | grep "jenkins" | awk '{print $1}'`
  if [ -n "$containerId" ]; then
    echo "删除容器jenkins!"
    docker stop jenkins
    docker rm jenkins
    containerId=`docker ps -a | grep "jenkins" | awk '{print $1}'`
    if [ -n "$containerId" ]; then
      echo "容器jenkins删除失败!请使用[docker logs]命令查看详细日志!"
    else
      echo "容器jenkins已删除!"
    fi
  else
    echo "容器jenkins已删除!"
  fi
else
  echo "正确使用脚本的方式:"
  echo "./jenkins start #启动jenkins,多次执行该命令并不会重复启动jenkins,请放心使用!"
  echo "./jenkins stop #停止jenkins"
  echo "./jenkins status #查看jenkins状态"
  echo "./jenkins remove #删除jenkins状态,无论jenkins处于什么状态!"
  echo "./jenkins help #查看脚本使用方式!"
fi

三. 附注说明

启动脚本的目录挂载部门, 要根据实际情况修改, 当前的只能做参考, 必须修改之后才能实际使用.

资源地址:
https://github.com/free-pan/docker-image

    原文作者:码农梦醒
    原文地址: https://www.jianshu.com/p/c0c29d25c4d5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞