Search code examples
dockergitlab-cigitlab-ci-runnerdocker-in-docker

Run docker commands from gitlab-ci


I have this gitlab-ci file:

services:
  - docker:18.09.7-dind
variables:
  SONAR_TOKEN: "$PROJECT_SONAR_TOKEN"
  GIT_DEPTH: 0
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

  DOCKER_HOST: "tcp://docker:2375"
  DOCKER_DRIVER: overlay2

sonarqube-check:
  image: maven:latest
  stage: test
  before_script:
    - "docker version"
    - "mkdir $PWD/.m2"
    - "cp -f /cache/settings.xml $PWD/.m2/settings.xml"
  script:
    - mvn $MAVEN_CLI_OPTS clean verify sonar:sonar -Dsonar.qualitygate.wait=true -Dsonar.login=$SONAR_TOKEN -Dsonar.projectKey="project-key"
  after_script:
    - "rm -rf $PWD/.m2"
  allow_failure: false
  only:
    - merge_requests

For some reason docker in docker service does not find the binaries for docker (the docker version command, line 16):

/bin/bash: line 111: docker: command not found

I'm wondering if there is a way of doing this inside of the gitlab-ci file because I need to run docker for the tests, if there is an image that contains both maven and docker binaries or if I'll have to create my own docker image.

It has to be all in one stage, I cannot divide it in two stages (or at least I don't know how to compile in maven in one stage and run the tests witha docker image in another stage)

Thank you!


Solution

  • As you correctly pointed out. You need mvn and docker binaries in the image you are using for that GitLab-CI job.

    The quickest win is probably to install docker in your maven:latest build image during run time in the before_script section.

    before_script:
        - apt-get update && apt-get install -y docker.io
        - docker version
    

    If that's slowing down your job too much you might want to build your own custom docker image that contains both Maven and Docker.

    Also have a look at the article about dind on Gitlab if you end up moving to Docker 19.03+