How to run docker CLI in the in a Container

Hi All,

I’m trying to run docker CLI in a container and connecting it to the Docker daemon on the host. I have register the gitlab runner to use docker and share /var/run/docker.sock and use the docker image in the .gitlab-ci.yml file. I’m receiving error during the start of the docker build.
Error:
time=“2019-12-13T11:31:29Z” level=error msg=“failed to dial gRPC: cannot connect to the Docker daemon. Is ‘docker daemon’ running on this host?: dial tcp: lookup docker on 169.254.169.254:53: no such host” 25 error during connect: Posthttp://docker:2375/v1.40/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&session=iewgymdujsnu3tx7z1mer1z3t&shmsize=0&t=registry.gitlab.com%2Fswapnilshelke%2Fspringbootapp&target=&ulimits=null&version=1: context canceled29 ERROR: Job failed: exit code 1

Thanks for your help

Hi,

the easiest way would be to use the Docker-in-Docker workflow, where you use the docker image inside your .gitlab-ci.yml and start the dind service.

What’s your use case with passing the socket? Can you share the existing CI config?

Cheers,
Michael

Hello @dnsmichi ,

Thanks for the reply, I have tried the docker-in-docker workflow that you are suggesting its working, Now i’m trying this approach https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-socket-binding bind-mount /var/run/docker.sock into container. I’m using Windows 10 Pro, Docker Toolbox (version 19.03.1) and Gitlab-runner (version:12.5.0) .

These are the CI config I’m running:

.gitlab-ci.yml

image: docker:19.03.1

stages:
  - build
  - package
  - deploy

maven-build:
  image: maven:3-jdk-8
  stage: build
  script: "mvn package -B"
  artifacts:
    paths:
      - target/*.jar

docker-build:
  stage: package
  script:
  - docker build -t registry.gitlab.com/swapnilshelke/springbootapp .
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
  - docker push registry.gitlab.com/swapnilshelke/springbootapp
  
docker-deploy:
  stage: deploy
  script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
  - docker pull registry.gitlab.com/swapnilshelke/springbootapp
  - docker run --name test -p 9090:8080 -i registry.gitlab.com/swapnilshelke/springbootapp

config.toml

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "docker runner"
  url = "https://gitlab.com/"
  token = "XXXXXXXXXXXXX"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "docker:19.03.1"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

But it seems like build container can not have access to docker sock. Is there any configuration I need to do?

Hi,

well, Windows fun … there are no unix domain sockets, and after googling a bit, I’ve found this entry. It says that you can access a named pipe instead of the unix socket. Also, it points to the Docker for Windows FAQ

Maybe the following environment variable helps in your .gitlab-ci.yml

variables:
  DOCKER_HOST: npipe:////./pipe/docker_engine

If the named pipe approach fails for some reason (could be that docker.exe is not running with it), try the following variable value tcp://docker.for.win.localhost:2375 (if that’s exposed in the Docker settings on your Windows client).

If may be the case that you also need to explicitly tell the docker CLI command where to connect to in your script sections if the variable has no effect.

Cheers,
Michael