0

I am trying to build a docker image as part of a gitlab ci job. I work on my own gitlab instance with a separate machine running ci jobs. I have tried and failed with many different configurations for using Docker in Docker. My current try is the method from this blog post: https://medium.com/@tonywooster/docker-in-docker-in-gitlab-runners-220caeb708ca

When executing a CI job on this runner with the docker command I get the following error:

ERROR: Failed to remove network for build
ERROR: Preparation failed: Cannot connect to the Docker daemon at tcp://gitlab-dind:2375. Is the docker daemon running? (docker.go:745:240s)

Why is that and how can I fix it? I am also happy with any other working method to build and push a docker container in my CI job. I am happy to provide any further information, but I am not sure what would help and don’t want to bloat the post.

As requested my gitlab CI file looks like this:

image: docker

stages:
  - package
  - test
  - push

build docker image:
  stage: package

  tags:
    - docker

  script: 
    - cat /proc/self/cgroup
    - docker info
    - docker build -t $CI_REGISTRY_IMAGE .

test docker image:
  stage: test

  tags:
    - docker

  script: 
    - docker run $CI_REGISTRY_IMAGE python testscript.py

push docker image:
  stage: push

  tags:
    - docker

  script:
    - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin
    - docker push $CI_REGISTRY_IMAGE
1
  • Show us your gitlab CI file Commented Jan 1, 2023 at 22:47

2 Answers 2

0

To build a docker image in Gitlab runner, I recommend using kaniko instead of "docker in docker". Please read the official document of kaniko with Gitlab runner.

https://docs.gitlab.com/ee/ci/docker/using_kaniko.html

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 3, 2023 at 16:20
0

I wrote how to perform Docker build in GitLab Self-Hosted Runners Demo article. It uses docker in docker approach.

.gitlab-ci.yml is below:

stages:          # List of stages for jobs, and their order of execution
  - build

services:  
  - name: docker:20.10.16-dind 
    command: ["--mtu=1300"]    


build:  
  tags: [docker-on-prem]
  image: docker:20.10.16
  stage: build
  variables:
    DOCKER_HOST: "tcp://docker:2376"
    DOCKER_TLS_CERTDIR: "/certs"
    DOCKER_TLS_VERIFY: "1"
    DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
  script:

    - docker build -t test .

Sample repository with Dockerfile which you can fork and try the pipeline is at https://gitlab.com/warrior7089/gitlab-self-hosted-runners-demo/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.