在Gitlab CI中连接到Docker守护程序的麻烦困难

发布于 2025-02-04 14:54:12 字数 929 浏览 4 评论 0原文

我正在尝试创建一个相当简单的gitlab ci文件来构建docker映像。每当我运行管道时,我最终都会遇到Docker守护程序连接问题。我该怎么做才能正确构建我的形象?谢谢!

gitlab ci:

image: docker:20.10.16

services:
  - docker:20.10.16-dind

variables:
  DOCKER_HOST: tcp://docker:2375

iac-build:
  stage: build
  extends: .iac
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      when: always
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
  script:
    - docker build -t testfirstimage .
  allow_failure: false

错误:

$ docker build -t testfirstimage .
failed to dial gRPC: cannot connect to the Docker daemon. Is 'docker daemon' running on this host?: dial tcp 127.0.0.1:2375: connect: connection refused
Cleaning up project directory and file based variables
ERROR: Job failed: command terminated with exit code 1

I'm trying to create a fairly simple GitLab CI file to build out Docker images. Whenever I run the pipeline, I end up getting a Docker daemon connection issue. What can I do to properly build my image? Thanks!

GitLab CI:

image: docker:20.10.16

services:
  - docker:20.10.16-dind

variables:
  DOCKER_HOST: tcp://docker:2375

iac-build:
  stage: build
  extends: .iac
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      when: always
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
  script:
    - docker build -t testfirstimage .
  allow_failure: false

Error:

$ docker build -t testfirstimage .
failed to dial gRPC: cannot connect to the Docker daemon. Is 'docker daemon' running on this host?: dial tcp 127.0.0.1:2375: connect: connection refused
Cleaning up project directory and file based variables
ERROR: Job failed: command terminated with exit code 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜`诱少女 2025-02-11 14:54:12
  1. 我要检查的第一件事是,您是否已经在该本地主机上运行了一些东西 - 我实际上尝试在本地主机端口上运行服务器数小时,并一直拒绝它,只是发现我忘记了终止我与该端口的连接。

  2. 如果不是问题,我以前有这个问题,必须运行此命令才能使其工作:


concurrent = 1
check_interval = 0

[[runners]]
  name = "#####"
  url = "#####"
  token = "#####"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = false
    disable_cache = false
    cache_dir = "cache"
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
  [runners.cache]
    Insecure = false

我花了永远尝试弄清楚它,直到我发现

volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]

我没有' t魔术弄清楚了 - 在此问题页面上提出: https:https:https:https:https: //gitlab.com/gitlab-org/gitlab-runner/-/issues/1986

希望这有效。

  1. First thing I would check is if you already have something running on that local host - I've literally tried running a server on a local host port for hours and kept having it refused, only to find out that I had forgotten to terminate my connection to that port.

  2. If that isn't the issue, I had this issue before and had to run this command to get it to work:


concurrent = 1
check_interval = 0

[[runners]]
  name = "#####"
  url = "#####"
  token = "#####"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = false
    disable_cache = false
    cache_dir = "cache"
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
  [runners.cache]
    Insecure = false

I spent forever trying to figure it out and couldn't get anything to work until I found out to add

volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]

I didn't figure it out by magic though - props to this issues page: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1986

Hopefully that works.

帅哥哥的热头脑 2025-02-11 14:54:12

为了让Docker-In-In-Docker与Gitlab CI一起工作,您首先需要决定是否要使用或不使用TLS使用Docker-In-in-Docker。然后,更改/etc/gitlab-runner/config.toml设置,然后在您的.gitlab-ci.yml文件中分配docker_tls_tls_certdir。请参阅 docker in gitlab文档的a>。

docker in-docker with tls

# /etc/gitlab-runner/config.toml

[[runners]]
  url = "https://gitlab.com/"
  token = TOKEN
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:20.10.16"
    privileged = true
    disable_cache = false
    volumes = ["/certs/client", "/cache"]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
# .gitlab-ci.yml

image: docker:20.10.16

variables:
  DOCKER_TLS_CERTDIR: "/certs"

services:
  - docker:20.10.16-dind

before_script:
  - docker info

# rest of .gitlab-ci.yml

In order to get Docker-in-Docker working with GitLab CI, you will first need to decide if you want to use Docker-in-Docker with or without TLS. Then, change /etc/gitlab-runner/config.toml settings, and assign the DOCKER_TLS_CERTDIR in your .gitlab-ci.yml file. See the Docker-in-docker section of the GitLab docs.

Docker-in-docker with TLS:

# /etc/gitlab-runner/config.toml

[[runners]]
  url = "https://gitlab.com/"
  token = TOKEN
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:20.10.16"
    privileged = true
    disable_cache = false
    volumes = ["/certs/client", "/cache"]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
# .gitlab-ci.yml

image: docker:20.10.16

variables:
  DOCKER_TLS_CERTDIR: "/certs"

services:
  - docker:20.10.16-dind

before_script:
  - docker info

# rest of .gitlab-ci.yml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文