docker安装jenkins

这个是给自己写的,启动,停止,删除脚本. 用于给自己以后参考的. 本人非运维, 所以, 因此这些东西, 现在虽然清楚, 但长时间不用. 又会忘记, 所以, 自己可自己在这留个备份.

# 重复执行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 8000:8080 -p 50000:50000 --name jenkins -v /Users/pan/docker/volume/jenkins:/var/jenkins_home org.pzy/jenkins-cn: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://www.jianshu.com/p/1e78df911d55
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞