CICD 工具

RedisLive

容器间通信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
version: '2'

services:
mysql:
container_name: mysql
restart: always
image: docker.io/mysql:ninemax
environment:
MYSQL_ROOT_PASSWORD: ninemax
volumes:
- ./mysql/:/var/lib/mysql/
ports:
- "3308:3306"
networks: # 注册网络
- mynet
benti-images:
build: .
container_name: benti-images
restart: always
image: benti-images:11.0
#links链接到其它服务中的容器,配置后benti-images容器就可以用mysql连接数据库,如:mysql:3306
links:
- mysql
ports:
- "8080:7080"
#tty: true 配置是为了容器保持后台运行,不退出。如果不加这个参数,benti-images容器会不断退出不断重启
tty: true
networks: # 配置连接到注册的网络,保证在同一个网络域中
- mynet
networks:
mynet:
driver: bridge

gitlab cicd docker-compose

  • Docker Compose 和 GitLab

  • .gitlab-ci.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    image: creatiwww/docker-compose:latest

    services:
    - docker:dind

    variables:
    STAGE_SERVER_IP: 10.10.10.1
    PROD_SERVER_IP: 10.10.10.2
    STAGE_SERVER_USER: gitlab
    PROD_SERVER_USER: gitlab
    STAGE_IMAGE_APP_TAG: registry.my_organiation.org/development/amazing_app/app:stage
    PROD_IMAGE_APP_TAG: registry.my_organiation.org/development/amazing_app/app:prod
    EXTERNAL_SERVICE_STAGE_API_BASE_URL: https://test.some-service.com/
    EXTERNAL_SERVICE_PROD_API_BASE_URL: https://some-service.com/

    stages:
    - build
    - staging
    - release
    - deploy

    build:
    stage: build
    script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "IMAGE_APP_TAG=$STAGE_IMAGE_APP_TAG" >> .env
    - docker-compose build
    - docker-compose push
    only:
    - pre-prod
    - master

    deploy-to-stage:
    stage: staging
    script:
    - eval $(ssh-agent -s)
    - echo "$SSH_STAGE_SERVER_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "EXTERNAL_SERVICE_KEY=$EXTERNAL_SERVICE_STAGE_KEY" >> .env
    - echo "IMAGE_APP_TAG=$STAGE_IMAGE_APP_TAG" >> .env
    - docker-compose -H "ssh://$STAGE_SERVER_USER@$STAGE_SERVER_IP" down --remove-orphans
    - docker-compose -H "ssh://$STAGE_SERVER_USER@$STAGE_SERVER_IP" pull
    - docker-compose -H "ssh://$STAGE_SERVER_USER@$STAGE_SERVER_IP" up -d
    only:
    - pre-prod
    - master

    release:
    stage: release
    script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "IMAGE_APP_TAG=$PROD_IMAGE_APP_TAG" >> .env
    - docker-compose build
    - docker-compose push
    only:
    - master

    deploy-to-prod:
    stage: deploy
    script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PROD_SERVER_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "EXTERNAL_SERVICE_KEY=$EXTERNAL_SERVICE_PROD_KEY" >> .env
    - echo "IMAGE_APP_TAG=$PROD_IMAGE_APP_TAG" >> .env
    - docker-compose -H "ssh://$PROD_SERVER_USER@$PROD_SERVER_IP" down --remove-orphans
    - docker-compose -H "ssh://$PROD_SERVER_USER@$PROD_SERVER_IP" pull
    - docker-compose -H "ssh://$PROD_SERVER_USER@$PROD_SERVER_IP" up -d
    only:
    - master
    when: manual