简体   繁体   中英

Gitlab CI - Build Docker Image With Shared Runner (cannot connect to Docker Daemon)

I am currently using Gitlab Shared Runners to build and deploy my project (at least I'm trying too !).

I have the gitlab-ci.yml below :

image: java:8-jdk

stages:
  - build
  - package

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - docker info

cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches
build:
  stage: build
  script:
    - ./gradlew build
  artifacts:
    paths:
      - build/libs/*.jar
    expire_in: 1 week
  only:
    - master

docker-build:
  image: docker:stable
  services:
   - docker:dind
  stage: package
  script:
    docker build -t registry.gitlab.com/my-project .
    docker push registry.gitlab.com/my-project

after_script:
  - echo "End CI"

First, build stage is doing great, but there is a problem with the second stage when I'm trying to build and push my docker image.

I get this log :

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

It seems that Gitlab is using a shared runner that can't build a docker image, but I don't know how I can change that. I cannot change the configuration of my runner, because I'm using shared runners. I also tried to put some tags to my second stages, in hope that a more suitable runner would have to take care of my job, but I'm still getting this error.

Thank you for your help.

I believe you need to set DOCKER_HOST to connect to the DinD running in another container:

docker-build:
  image: docker:stable
  services:
   - docker:dind
  stage: package
  script:
    - export DOCKER_HOST=tcp://docker:2375/
    - docker build -t registry.gitlab.com/my-project .
    - docker push registry.gitlab.com/my-project

If your shared runner executor is of type docker you may try this setup :

stages:
  - build
  - package

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - docker info

cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches
build:
  image: java:8-jdk
  stage: build
  script:
    - ./gradlew build
  artifacts:
    paths:
      - build/libs/*.jar
    expire_in: 1 week
  only:
    - master

docker-build:
  stage: package
  script:
    docker build -t registry.gitlab.com/my-project .
    docker push registry.gitlab.com/my-project

after_script:
  - echo "End CI"

Even we have faced the same problem in our org. We found that there is a long standing issue with the docker in docker area for gitlab which can be tracked in these issues #3612 , #2408 and #2890 as well.

We have found that in our case using docker binding was much suitable for our usecase than the docker-in-docker one. so, we used the solution in their official page .

I know this has been already answered but this may help some one who have a similar usecase :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM