PurplePanda
PurplePanda

Reputation: 278

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.

Upvotes: 4

Views: 2762

Answers (3)

Leela Venkatesh K
Leela Venkatesh K

Reputation: 1730

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 :)

Upvotes: 1

Sébastien Helbert
Sébastien Helbert

Reputation: 2210

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"

Upvotes: 1

BMitch
BMitch

Reputation: 265150

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

Upvotes: 7

Related Questions