docker nginx jenkins

安装nginx、jenkins

# alpine 发行版
docker pull nginx:alpine  #安装Nginx
docker pull jenkins/jenkins:alpine   #安装Jenkins
# debian 版
# docker pull nginx  #安装Nginx
# docker pull jenkins/jenkins:lts   #安装Jenkins

创建挂载目录

docker
├── compose
│   └── docker-compose.yml  #docker-compose配置文件
├── jenkins
│   └── jenkins_home  #Jenkins挂载卷
├── nginx
    └── default.conf  #Nginx配置

创建docker-compose.yml文件

具体配置

version: '3'
services:                                      
  docker_nginx:
    restart: always
    image: nginx:alpine
    container_name: nginx-container
    ports:
      - 80:80
      - 6002:6002
    volumes:
      - nginx_conf:/etc/nginx/conf.d  # nginx会使用此文件夹内的.conf文件配置
      - nginx_html:/usr/share/nginx/html # 用我们创建的Nginx配置去替换容器中的网页文件
      - nginx_logs:/var/log/nginx  # nginx日志位置
    environment:
      - TZ=Asia/Shanghai
    networks:
      - frontend
      - backend
    dns:
      - 8.8.8.8
      - 8.8.4.4
  docker_jenkins:
    user: root
    restart: always
    image: jenkins/jenkins:lts-jdk11              # 指定服务所使用的镜像 
    container_name: jenkins-container          # 容器名称
    ports:                                     # 对外暴露的端口定义
      - 8080:8080
      - 50000:50000
    volumes:                                   # 卷挂载路径
      - jenkins_home:/var/jenkins_home  #冒号前为使用的volume名
      #- /var/run/docker.sock:/var/run/docker.sock
      #- /usr/bin/docker:/usr/bin/docker
      #- /usr/local/bin/docker-compose:/usr/local/bin/docker-compose
    environment:
      - TZ=Asia/Shanghai # 设置容器时区为上海
      - JENKINS_OPTS="--prefix=/jenkins" # 设置前缀,nginx反代理
      - JENKINS_ARGS="--prefix=/jenkins" 
    dns:
      - 8.8.8.8
      - 8.8.4.4
    networks:
      - backend
  docker_xray:
    image: teddysun/xray
    restart: always
    container_name: xray-container
    ports:
      - 443:433
    volumes:
      - xray:/etc/xray  # 需要放一个config.json文件
    environment:
      - TZ=Asia/Shanghai
    dns:
      - 8.8.8.8
      - 8.8.4.4
    networks:
      - frontend
volumes:
  jenkins_home: {}
  nginx_conf: {}
  nginx_html: {}
  nginx_logs: {}
  xray: {}

networks:
  frontend:
  backend:

启动

# 执行命令目录下需要有docker-compose.yml文件
docker-compose up -d

# 指定使用的yml文件
docker-compose -f useVolume.yml up -d

备份与迁移jenkins

  1. 备份

    # 查看已挂载卷
    docker volume ps
    # 查看volume位置
    docker volume inspect docker_jenkins_home
    # 进到文件夹
    cd /var/lib/docker/volumes
    # 打包docker_jenkins_home文件夹
    tar -zcvf jenkins_home -C docker_jenkins_home . # 记得加.(用于去掉绝对路径)
    
  2. 恢复

    # 进到文件夹
    cd /var/lib/docker/volumes/docker_jenkins_home
    # 删除_data原有文件
    rm -rf _data
    # 解压
    tar -zxvf jenkins_home.tar.gz
    
  3. 修改jenkins location 登录jenkins -> 系统管理 -> 全局配置 -> Jenkins URL 改为 ip:8080/
    不改系统管理界面可能会报反向代理设置有误

docker源

https://updates.jenkins.io/update-center.json
http://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
http://updates.jenkins-ci.org/update-center.json
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
http://mirror.esuni.jp/jenkins/updates/update-center.json
http://mirror.xmission.com/jenkins/updates/update-center.json

错误记录

  1. 添加volumes失败 but no declaration was found in the volumes section.: yml中添加顶级变量

    volumes:
      jenkins_home: {}
    
  2. jekins 显示离线,或者插件安装不上

    1. 墙的问题:更换镜像源或离线安装open in new window
    2. docker容器本身网络问题:docker-compose.yml中增加/修改dns,其他方案open in new window
  3. jenkins使用alpine版 nodejs 报错 node: not found,简单解决:不用alpine。其他解决办法open in new window

  4. 钉钉机器人构建后不发消息提示java.lang.NullPointerException,解决办法:dingtalk插件暂时替换为2.4.11,替换方式open in new window

参考链接

1open in new window

Last Updated:
Contributors: haizhiguang