gitlab runner docker in docker 踩坑记

2019-08-14 00:11:34
收藏数:0
阅读数:47
评论数:3

gitlab docker in docker 踩坑记

问题

docker: Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?.

runner 配置

/etc/gitlab-runner/config/config.toml

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  environment = ["DOCKER_TLS_CERTDIR="] # 需要配置
  name = "hejunwei-macbook"
  url = "http://115.28.82.133:9999/"
  token = "token"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = true # 需要配置
    disable_cache = false
    volumes = ["/cache"] # 我们用的是 docker in docker (dind) 不需要映射 /var/run/docker.sock
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.custom]
    run_exec = ""

.gitlab-ci.yml 配置

variables:
  DOCKER_DRIVER: overlay
  DOCKER_HOST: tcp://docker:2375

services:
  - docker:dind

stages:
  - build
  - deploy

master-build:
  stage: build
  image: docker:stable
  script:
  - docker login -u horan -p $DOCKER_PASSWORD https://index.docker.io/v1/ # dockerhub 的 url
  - docker run --rm -v $CI_PROJECT_DIR:/go/src/api.bangshang.net.cn -w /go/src/api.bangshang.net.cn -e CGO_ENABLED=0 -e=GOOS=linux -e GOPROXY=https://mirrors.aliyun.com/goproxy/ -e GO111MODULE=on golang:1.12 go build -a -ldflags '-extldflags "-static"' . # scratch 镜像没有任何依赖,所以需要静态编译
  - sed "s/APP_ENV=/APP_ENV=production/;s/APP_DEBUG=/APP_DEBUG=false/;s/DB_HOST=/DB_HOST=172.17.0.5/;s/REDIS_HOST=/REDIS_HOST=172.17.0.5/;s/DB_PASSWORD=/DB_PASSWORD=$MYSQL_PASSWORD/" .env.example > .env
  - docker build -t horan/api.bangshang.net.cn:$CI_COMMIT_SHA .
  - docker push horan/api.bangshang.net.cn:$CI_COMMIT_SHA
  only:
  - master

master-deploy:
  stage: deploy
  image: kroniak/ssh-client
  script:
  - mkdir ~/.ssh
  - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
  - echo $SSH_PRIVATE_KEY | base64 -d > ~/.ssh/id_rsa # 这里使用 base64 编码一下,gitlab 的变量直接读取会有问题
  - chmod 600 ~/.ssh/id_rsa # 这里权限必须是 600
  - ssh ubuntu@49.234.41.197 'cd /home/ubuntu/api-bangshang && git pull && go build'
  only:
  - master
horan
horan
runner的注册过程可以看gitlab官方文档
2019-08-24 13:34:08
horan
horan
gitlab 本身也可以用来docker 部署
2019-08-24 18:23:12
horan
horan
1
2019-08-24 18:24:19