docker-compose镜像版本动态控制

提取.env文件进行配置通用环境变量

# 当前机器用户的home路径
HOST_HOME=/home/guimu
# 上传文件临时路径
TMP_DATA_PATH=${HOST_HOME}/tempdata/
# media的home路径
MEDIA_HOME=/media
# 挂载的mysql的data路径
MYSQL_DATA_PATH=${HOST_HOME}/mysql/data
# mysql配置文件
MYSQL_CONF=${HOST_HOME}/mysql/my.cnf
#redis数据
REDIS_DATA_PATH=${HOST_HOME}/redis/data
#redis配置文件
REDIS_CONFIG=${HOST_HOME}/redis/redis.conf
# geoserver路径
GEO_SERVER_HOME=${HOST_HOME}/geoserver
# mysql数据库的用户名
DB_USER=root
# mysql数据库的密码
DB_PASSWORD=password
# mysql数据库的host, 一般不用修改,除非docker-compose中的mysql容器名修改才进行修改
DB_HOST=das-mysql
# 当前机器的ip(一般用外网能访问的ip)
HOST_IP=localhost
# 用于测试打包新版本服务镜像, 需要打包新镜像版本时使用
VERSION=latest
# 图像雷达服务日志路径
IMG_RADAR_LOG_PATH=${HOST_HOME}/img_radar_log

docker-compose编排

创建docker-compose-dev.yml:

version: '3'
networks:
  guimu-micro-service-platform_default:
services:
  xxl-job-admin:
    image: kven/xxl-job-admin:2.3.0
    restart: always
    container_name: xxl-job-admin
    environment:
      PARAMS:
        '
              --server.port=9090
              --server.servlet.context-path=/xxl-job-admin
              --spring.datasource.url=jdbc:mysql://${DB_HOST}:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
              --spring.datasource.username=${DB_USER}
              --spring.datasource.password=${DB_PASSWORD}'
    ports:
      - 9090:9090
    networks:
      - guimu-micro-service-platform_default
  geoserver:
    restart: always
    image: kven/geoserver:3.0
    container_name: geoserver
    volumes:
      - ${GEO_SERVER_HOME}/data_dir:/usr/local/geoserver/data_dir
      - /usr/share/fonts/:/usr/share/fonts/ # 此处将宿主机字体映射到容器中,解决发布图层中文乱码问题
    ports:
      - "9180:9180"
    networks:
      - guimu-micro-service-platform_default
  #注册中心
  #自定义的服务名称
  service-registry:
    build:
      context: das-service-registry
      dockerfile: Dockerfile
    #    #镜像名称
    image: kven/service-registry:${VERSION}
    #    #容器名称
    container_name: service-registry
    environment:
      JAVA_OPTS: -Xms256m -Xmx256m
    #容器down掉后,会自动重启
    restart: always
    #映射端口。格式为宿主机端口:容器端口
    ports:
      - "9185:9185"
    networks:
      - guimu-micro-service-platform_default

  #  das-ai-service:
  #    build:
  #      context: das-ai-service
  #      dockerfile: Dockerfile
  #    image: das-ai-service
  #    container_name: das-ai-service
  #    restart: always
  #    ports:
  #      - "9111:9111"
  #    depends_on:
  #      - service-registry
  #    networks:
  #      - guimu-micro-service-platform_default

  #网关服务
  api-gateway-zuul:
    build:
      context: das-service-gateway
      dockerfile: Dockerfile
    image: kven/api-gateway-zuul:${VERSION}
    container_name: api-gateway-zuul
    environment:
      JAVA_OPTS: -Xms256m -Xmx256m
    restart: always
    ports:
      - "9100:9100"
    depends_on:
      - service-registry
    networks:
      - guimu-micro-service-platform_default
  das-data-processed-service:
    build:
      context: das-data-mgr-processed
      dockerfile: Dockerfile
    image:  kven/das-data-processed-service:${VERSION}
    container_name: das-data-processed-service
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      IMG_HOST: imgcentos
    #      DB_PORT: 3306 # 不配置,默认为3306端口
    # 挂载的路径需要根据后面迁移的机器进行挂载
    volumes:
      - ${MEDIA_HOME}:/media/
      - ${TMP_DATA_PATH}:/home/das/tempdata/
    restart: always
    ports:
      - "9109:9109"
    depends_on:
      - service-registry
      - xxl-job-admin
    networks:
      - guimu-micro-service-platform_default
  das-data-gis-service:
    build:
      context: das-data-mgr-gis
      dockerfile: Dockerfile
    image: kven/das-data-gis-service:${VERSION}
    container_name: das-data-gis-service
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      JAVA_OPTS: -Xms256m -Xmx256m
    # 挂载的路径需要根据后面迁移的机器进行挂载
    volumes:
      - ${MEDIA_HOME}:/media/
      - ${TMP_DATA_PATH}:/home/das/tempdata/
    restart: always
    ports:
      - "9108:9108"
    depends_on:
      - service-registry
      - xxl-job-admin
    networks:
      - guimu-micro-service-platform_default
  das-data-raw-service:
    build:
      context: das-data-mgr-raw
      dockerfile: Dockerfile
    image: kven/das-data-raw-service:${VERSION}
    container_name: das-data-raw-service
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      JAVA_OPTS: -Xms256m -Xmx256m
    # 挂载的路径需要根据后面迁移的机器进行挂载
    volumes:
      - ${MEDIA_HOME}:/media/
    restart: always
    ports:
      - "9107:9107"
    depends_on:
      - service-registry
      - xxl-job-admin
    networks:
      - guimu-micro-service-platform_default
  das-security-service:
    build:
      context: das-service-security
      dockerfile: Dockerfile
    image: kven/das-security-service:${VERSION}
    container_name: das-security-service
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      JAVA_OPTS: -Xms256m -Xmx256m
    restart: always
    ports:
      - "9110:9110"
    depends_on:
      - service-registry
    networks:
      - guimu-micro-service-platform_default
  das-ui-service:
    build:
      context: das-web-ui
      dockerfile: Dockerfile
    image: kven/das-ui-service:${VERSION}
    container_name: das-ui-service
    environment:
      GATE_HOST: ${HOST_IP}
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      JAVA_OPTS: -Xms256m -Xmx256m
    # 挂载的路径需要根据后面迁移的机器进行挂载
    volumes:
      - ${MEDIA_HOME}:/media/
    restart: always
    ports:
      - "9103:9103"
    depends_on:
      - service-registry
    networks:
      - guimu-micro-service-platform_default
  das-workflow-service:
    build:
      context: das-workflow-core
      dockerfile: Dockerfile
    image: kven/das-workflow-service:${VERSION}
    container_name: das-workflow-service
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      JAVA_OPTS: -Xms256m -Xmx256m
    restart: always
    ports:
      - "9101:9101"
    depends_on:
      - service-registry
    networks:
      - guimu-micro-service-platform_default
  mysql:
    restart: always
    container_name: das-mysql
    image: kven/mysql:1.0
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
    volumes:
      - ${MYSQL_DATA_PATH}:/var/lib/mysql
    networks:
      - guimu-micro-service-platform_default
  redis:
    restart: always
    container_name: das-redis
    image: redis:6.2.7
    ports:
      - "6380:6379"
    volumes:
      - ${REDIS_DATA_PATH}:/data
      - ${REDIS_CONFIG}:/etc/redis/redis.conf
    networks:
      - guimu-micro-service-platform_default
  imgserver:
    image: kven/imgcentos:4.0
    container_name: imgcentos
    restart: always
    ports:
      - "9140:9140"
    volumes:
      - ${MEDIA_HOME}:/media
      - ${IMG_RADAR_LOG_PATH}:/root/log
    networks:
      - guimu-micro-service-platform_default
  radarserver:
    image: kven/radarcentos:2.0
    container_name: radarcentos
    restart: always
    ports:
      - "9130:9130"
    volumes:
      - ${MEDIA_HOME}:/media
      - ${IMG_RADAR_LOG_PATH}:/root/log
    networks:
      - guimu-micro-service-platform_default

# 修改.env文件中的version配置打包对应的版本镜像, 指定文件docker-compose-dev.yml构建dev的镜像

docker-compose -f docker-compose-dev.yml up --build -d

推送构建的镜像到远程仓库

# 首先需要登录对应的远程仓库, 输入指定的账号密码

docker login

编写对应的推送脚本pushImage.sh:

#!/bin/bash
version=$1

commands="docker push kven/das-workflow-service:${version};
docker push kven/das-ui-service:${version};
docker push kven/das-security-service:${version};
docker push kven/das-data-raw-service:${version};
docker push kven/das-data-gis-service:${version};
docker push kven/das-data-processed-service:${version};
docker push kven/api-gateway-zuul:${version};
docker push kven/service-registry:${version};"

bash -c "$commands"

# 设定可执行权限

chmod +x pushImage.sh

# 推送指定的版本:

# ./pushImage.sh 版本号

./pushImage.sh latest

配置Jenkins

新建工程

docker 使用Dockerfile更新jenkins镜像 docker-compose 更新镜像_镜像升级

 配置工程名称, 选择freestyle project,点击确定

docker 使用Dockerfile更新jenkins镜像 docker-compose 更新镜像_mysql_02

 支持参数化构建

docker 使用Dockerfile更新jenkins镜像 docker-compose 更新镜像_jenkins_03

 通过shell构建

docker 使用Dockerfile更新jenkins镜像 docker-compose 更新镜像_jenkins_04

 在对应的构建服务器上编写restart.sh脚本:

#!/bin/bash
# 切换到对应的工程目录
cd /home/guimu/das-docker/guimu-micro-service-platform-internal
# 更新代码
svn up
# 打包
mvn package -DskipTest
# 关闭原有的容器
docker-compose -f docker-compose-dev.yml down
# 构建新的容器并启动
docker-compose -f docker-compose-dev.yml up --build -d

# 添加可执行权限

chmod +x restart.sh

在前一步中的Jenkins添加的shell中编写脚本:

#!/bin/sh
# 切换到工程的工作空间
cd /home/guimu/das-docker
# 根据Jenkins的构建参数version动态替换.env文件中对应的版本号
sed -i "s/VERSION=\(.*\)/VERSION=${version}/g" /home/guimu/das-docker/guimu-micro-service-platform-internal/.env
# 重启并构建新版本的容器镜像
./restart.sh
# 推送对应的版本镜像
./pushImage.sh $version