Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

gitlab

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.

like image 350
PurplePanda Avatar asked Jan 03 '23 01:01

PurplePanda


1 Answers

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
like image 104
BMitch Avatar answered Jan 05 '23 17:01

BMitch