Search code examples
dockergitlab-ci

Docker-in-docker: Cannot connect to daemon


In my GitLab CI pipeline, I have a job which tests my code using a combination of pytest and docker-compose (my pytest fixture runs docker-compose). So, I have something like

test job:
  image: ubuntu:20.04
  services:
    - docker:dind
  script:
    - apt -y update
    - apt -y install python3-pip docker-compose
    - ...

However, when I try to run various Docker commands, I get

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

How can I connect to the socket? When I switch from ubuntu:20.04 to docker:latest, it works. However, I don't get access to any of the Debian packages.


Solution

  • See https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#docker-in-docker-with-tls-enabled-in-the-docker-executor :

      # When you use the dind service, you must instruct Docker to talk with
      # the daemon started inside of the service. The daemon is available
      # with a network connection instead of the default
      # /var/run/docker.sock socket. Docker 19.03 does this automatically
      # by setting the DOCKER_HOST in
      # https://github.com/docker-library/docker/blob/d45051476babc297257df490d22cbd806f1b11e4/19.03/docker-entrypoint.sh#L23-L29
    

    Other containers do not set it automatically. Set DOCKER_HOST yourself.

    variables:
      DOCKER_HOST: tcp://docker:2375
      DOCKER_TLS_CERTDIR: "/certs"