Connecting to docker-in-docker from a GitLab CI runner

Craig Otis picture Craig Otis · Jul 26, 2017 · Viewed 7.3k times · Source

I have a GitLab pipeline that I want to:

  1. Build a Java app
  2. Test using docker-compose
  3. Push to my Docker repository

The primary issue I'm having is that this works:

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: docker:latest
  script:
  - docker version

The output is printed as expected:

> gitlab-ci-multi-runner exec docker --docker-privileged docker_test
...
$ docker version
Client:
 Version:      17.06.0-ce
...
Server:
 Version:      17.06.0-ce
...
Build succeeded

While this does not (installation steps for docker-ce omitted):

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: ubuntu:latest       << note change
  script:
  - docker version

It fails with:

$ docker version
Client:
 Version:      17.06.0-ce
 API version:  1.30
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:23:31 2017
 OS/Arch:      linux/amd64
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ERROR: Build failed: exit code 1
FATAL: exit code 1

How do I make my ubuntu image (or whatever image is going to build my project) connect to the linked Docker-in-Docker service? What is docker:latest doing that I'm not?

I've read up on the GitLab services documentation, but it only makes sense to me from a hostname perspective. (If you have a mysql service, you can connect over mysql:3306.)

Edit: Updating the command to echo $DOCKER_HOST, I see in the docker:latest image:

$ echo $DOCKER_HOST
tcp://docker:2375

And in the ubuntu:latest image I see:

$ echo $DOCKER_HOST
(nothing - but SO doesn't let me add a blank code line)

Answer

Robert picture Robert · Jul 28, 2017

As the information you've added, I hope that this does work:

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: ubuntu:latest
  variables:
      DOCKER_HOST: "tcp://docker:2375"
  script:
  - docker version

Alternatively:

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: ubuntu:latest 
  script:
  - export DOCKER_HOST=tcp://docker:2375
  - docker version

It seems that Gitlab does not set the DOCKER_HOST variable for custom images.