Docker 基于 Compose 的 Gitlab CI - 管道错误

Docker Compose based Gitlab CI - Pipe error

问题

我用 docker compose 做了一个项目。它在本地主机上运行良好。我想使用这个基础来使用 Gitlab Runner 测试或分析代码。我解决了很多问题,比如在容器中安装 docker compose、运行 和构建选定的容器以及 运行 命令。第一份工作 运行 并成功 (!!!),但以下工作在 "before_script" 之前失败:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
...
Error response from daemon: Conflict.
...
Error response from daemon: Conflict.

我不明白为什么。我做错了什么?我再说一遍:管道的第一个作业 运行s 以及 "success" 消息!管道的每个其他作业都失败。

完整输出:

Running with gitlab-ci-multi-runner 9.4.0 (ef0b1a6)
  on XXX Runner (fdc0d656)
Using Docker executor with image docker:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Using docker image docker:dind ID=sha256:5096e5a0cba00693905879b09e24a487dc244b56e8e15349fd5b71b432c6ec9ffor docker service...
ERROR: Preparation failed: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Will be retried in 3s ...
Using Docker executor with image docker:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Using docker image docker:dind ID=sha256:5096e5a0cba00693905879b09e24a487dc244b56e8e15349fd5b71b432c6ec9f for docker service...
ERROR: Preparation failed: Error response from daemon: Conflict. The container name "/runner-fdc0d656-project-35-concurrent-0-docker" is already in use by container "80918876ffe53e33ce1f069e6e545f03a15469af6596852457f11dbc7a6c5b58". You have to remove (or rename) that container to be able to reuse that name.
Will be retried in 3s ...
Using Docker executor with image docker:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Using docker image docker:dind ID=sha256:5096e5a0cba00693905879b09e24a487dc244b56e8e15349fd5b71b432c6ec9f for docker service...
ERROR: Preparation failed: Error response from daemon: Conflict. The container name "/runner-fdc0d656-project-35-concurrent-0-docker" is already in use by container "80918876ffe53e33ce1f069e6e545f03a15469af6596852457f11dbc7a6c5b58". You have to remove (or rename) that container to be able to reuse that name.
Will be retried in 3s ...
ERROR: Job failed (system failure): Error response from daemon: Conflict. The container name "/runner-fdc0d656-project-35-concurrent-0-docker" is already in use by container "80918876ffe53e33ce1f069e6e545f03a15469af6596852457f11dbc7a6c5b58". You have to remove (or rename) that container to be able to reuse that name.

文件

.gitlab-ci.yml

# Select image from https://hub.docker.com/r/_/php/
image: docker:latest

# Services
services:
    - docker:dind

stages:
    - build
    - test
    - deploy

cache:
    key: ${CI_BUILD_REF_NAME}
    untracked: true
    paths:
        - vendor
        - var

variables:
    DOCKER_CMD: docker exec --user user bin
    COMPOSE_HTTP_TIMEOUT: 300

before_script:
    - apk add --no-cache py-pip bash
    - pip install docker-compose
    - touch ~/.gitignore
    - bin/docker-init.sh
    - cp app/config/parameters.gitlab-ci.yml app/config/parameters.yml
    - cp app/config/nodejs_parameters.yml.dist app/config/nodejs_paramteres.yml
    - chmod -R 777 app/cache app/logs var
    # Load only binary and mysql
    - docker-compose up -d binary mysql

build:
    stage: build
    script:
        - ${DOCKER_CMD} composer install -n
        - ${DOCKER_CMD} php app/console doctrine:database:create --env=test --if-not-exists
        - ${DOCKER_CMD} php app/console doctrine:migrations:migrate --env=test

codeSniffer:
    stage: test
    script:
        - ${DOCKER_CMD} bin/php-cs-fixer fix --dry-run --config-file=.php_cs

database:
    stage: test
    script:
        - ${DOCKER_CMD} php app/console doctrine:mapping:info --env=test
        - ${DOCKER_CMD} php app/console doctrine:schema:validate --env=test
        - ${DOCKER_CMD} php app/console doctrine:fixtures:load --env=test

unittest:
    stage: test
    script:
        - ${DOCKER_CMD} bin/phpunit -c app --debug

deploy_demo:
    stage: deploy
    script:
        - echo "Deploy to staging server"
    environment:
        name: staging
        url: https://staging.example.com
    only:
        - develop

deploy_prod:
    stage: deploy
    script:
        - echo "Deploy to production server"
    environment:
        name: production
        url: https://example.com
    when: manual
    only:
        - master

docker-compose.yml

version: "2"

services:
    web:
        image: nginx:latest
        ports:
            - "${HTTP_PORT}:80"
        depends_on:
            - mysql
            - elasticsearch
            - binary
        links:
            - binary:php
        volumes:
            - ".:/var/www"
            - "./app/config/docker/vhost.conf:/etc/nginx/conf.d/site.conf"
            - "${BASE_LOG_DIR}/nginx:/var/log/nginx"

    mysql:
        image: mysql:5.6
        environment:
            MYSQL_USER: test
            MYSQL_PASSWORD: test
            MYSQL_ROOT_PASSWORD: test
        ports:
            - "${MYSQL_PORT}:3306"
        volumes:
            - "${BASE_LOG_DIR}/mysql:/var/log/mysql"
            - "${BASE_MYSQL_DATA_DIR}:/var/lib/mysql"
            - "./app/config/docker/mysql.cnf:/etc/mysql/conf.d/mysql.cnf"

    elasticsearch:
        image: elasticsearch:1.7.6
        ports:
            - "${ELASTICSEARCH_PORT}:9200"
        volumes:
            - "${BASE_ELASTICSEARCH_DATA_DIR}:/usr/share/elasticsearch/data"

    binary:
        image: fchris82/kunstmaan-test
        container_name: bin
        volumes:
            - ".:/var/www"
            - "${BASE_LOG_DIR}/php:/var/log/php"
            - "~/.ssh:/home/user/.ssh"
        tty: true
        environment:
            LOCAL_USER_ID: ${LOCAL_USER_ID}

config.toml

[[runners]]
  name = "XXX Runner"
  url = "https://gitlab.xxx.xx/"
  token = "xxxxxxxxxxx"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = true
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
    shm_size = 0
  [runners.cache]

好的,我找到问题了。我破坏了配置。如果您在 .gitlab-ci.yml 中使用 dind 服务,则不要在 config.toml 文件中使用 /var/run/docker.sock 卷,反之亦然,如果您使用 "socket"方法,不要使用 dind 服务。

更多信息:https://docs.gitlab.com/ce/ci/docker/using_docker_build.html