Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 919
Craig Otis Avatar asked Jul 26 '17 01:07

Craig Otis


People also ask

Does GitLab runner use Docker?

GitLab Runner can use Docker to run jobs on user provided images.

How does Docker integrate with GitLab?

Run your CI/CD jobs in Docker containers. For example, you can tell GitLab CI/CD to use a Node image that's hosted on Docker Hub or in the GitLab Container Registry. Your job then runs in a container that's based on the image. The container has all the Node dependencies you need to build your app.

What is the default Docker image for GitLab runner?

To use GitLab Runner with Docker you need to register a runner that uses the Docker executor. The registered runner uses the ruby:2.6 Docker image and runs two services, postgres:latest and mysql:latest , both of which are accessible during the build process.


1 Answers

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.

like image 72
Robert Avatar answered Sep 25 '22 02:09

Robert