如何在Gitlab CI中的Docker中运行Docker?

问题描述

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

[[runners]]
  name = "My Docker Runner"
  url = "###"
  token = "###"
  executor = "docker"
  pre_clone_script = "git config --global http.sslVerify false"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "tianGolo/docker-with-compose"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"]
    shm_size = 0
  [runners.cache]
    Insecure = false
    [runners.cache.s3]
    [runners.cache.gcs]

.gitlab-ci.yml

stages:
  - test

before_script:
  - apk update
  - apk upgrade
  - apk add make

test:
  stage: test
  tags: [test]
  script:
    - make prod-up # docker-compose up -d --build
    - make composer-init-dev
    - make test

当我在gitlab中运行脚本时,出现错误

$ make生产 docker-compose -f docker-compose.prod.yml up -d --build 无法在http:// docker:2375连接到Docker守护程序-它正在运行吗?

如果它位于非标准位置,请使用DOCKER_HOST环境变量指定URL。 make:*** [Makefile:16:docker-prod-up]错误1 错误:作业失败:退出代码2

请帮助,我在做什么错了?

解决方法

您缺少Docker服务(最好明确指定映像):

stages:
  - test

before_script:
  - apk update
  - apk upgrade
  - apk add make

test:
  stage: test
  image: tiangolo/docker-with-compose:latest
  services:
  - docker:dind
  tags: [test]
  script:
    - make prod-up # docker-compose up -d --build
    - make composer-init-dev
    - make test
,

尝试安装docker.sock:

test:
  stage: test
  tags: [test]
  script:
    - make prod-up # docker-compose up -d --build
    - make composer-init-dev
    - make test
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock