Answers: <p>To build a docker image in Gitlab runner, I recommend using kaniko instead of &quot;docker in docker&quot;.
  Please read the official document of kaniko with Gitlab runner.</p>
  <p><a href="https://docs.gitlab.com/ee/ci/docker/using_kaniko.html" rel="nofollow noreferrer">https://docs.gitlab.com/ee/ci/docker/using_kaniko.html</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2023-01-03T16:20:14.197Z
Answers: 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](/help/how-to-answer).
Answers: Community
Answers: CC BY-SA 4.0
Answers: 2023-01-03T09:38:24.497Z
Answers: 2023-01-03T09:39:43.377Z
Answers: 2023-01-03T09:39:43.377Z
Answers: Lawrence K.
Answers: <p>I wrote how to perform Docker build in <a href="https://www.rokpoto.com/gitlab-self-hosted-runners-demo/" rel="nofollow noreferrer">GitLab Self-Hosted Runners Demo</a> article. It uses docker in docker approach.</p>
  <p><code>.gitlab-ci.yml</code> is below:</p>
  <pre><code>stages:          # List of stages for jobs, and their order of execution
    - build
  
  services:  
    - name: docker:20.10.16-dind 
      command: [&quot;--mtu=1300&quot;]    
  
  
  build:  
    tags: [docker-on-prem]
    image: docker:20.10.16
    stage: build
    variables:
      DOCKER_HOST: &quot;tcp://docker:2376&quot;
      DOCKER_TLS_CERTDIR: &quot;/certs&quot;
      DOCKER_TLS_VERIFY: &quot;1&quot;
      DOCKER_CERT_PATH: &quot;$DOCKER_TLS_CERTDIR/client&quot;
    script:
  
      - docker build -t test .
  </code></pre>
  <p>Sample repository with Dockerfile which you can fork and try the pipeline is at <a href="https://gitlab.com/warrior7089/gitlab-self-hosted-runners-demo/" rel="nofollow noreferrer">https://gitlab.com/warrior7089/gitlab-self-hosted-runners-demo/</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2023-01-26T16:06:26.137Z
Answers: 2023-01-26T16:11:38.700Z
Answers: 2023-01-26T16:11:38.700Z
Answers: rokpoto.com
Body: <p>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: <a href="https://medium.com/@tonywooster/docker-in-docker-in-gitlab-runners-220caeb708ca" rel="nofollow noreferrer">https://medium.com/@tonywooster/docker-in-docker-in-gitlab-runners-220caeb708ca</a></p>
  <p>When executing a CI job on this runner with the docker command I get the following error:</p>
  <pre><code>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)
  </code></pre>
  <p>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.</p>
  <p>As requested my gitlab CI file looks like this:</p>
  <pre><code>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
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2023-01-01T22:47:04.760Z
Comments: Show us your gitlab CI file
Comments: Chris Doyle
Tags: <docker><gitlab><gitlab-ci-runner><docker-in-docker>
Title: Building a docker container in a gitlab ci job
UserDisplayName: Henrik
ThreadText: # Building a docker container in a gitlab ci job
  
  (Asked by: username_0 on 2023-01-01)
  
  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
  
  ```
  
  **Tags:** docker, gitlab, gitlab-ci-runner, docker-in-docker
  
  > **Comments:**
  > **username_1**: Show us your gitlab CI file  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2023-01-03. Score: 0 | Accepted answer
  
  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>
  
  > **Comments:**
  > **username_3**: 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](/help/how-to-answer).  
  
  ---
  
  ### Answer by username_4 on 2023-01-26. Score: 0
  
  I wrote how to perform Docker build in [GitLab Self-Hosted Runners Demo](https://www.username_4/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/>
  
  
  ---
  
  