无法连接到tcp://localhost:2375/上的Docker守护程序,Docker守护程序是否正在运行,在GitLab上

mkshixfv  于 2023-01-16  发布在  Docker
关注(0)|答案(5)|浏览(242)

我正在尝试在GitLab中构建CI管道。我想问一下如何让Docker在GitLab CI中工作。
本期:www.example.comhttps://gitlab.com/gitlab-org/gitlab-runner/issues/4501#note_195033385
两种方法我都按照说明操作。使用TLS和不使用TLS。但它仍然卡住。这是相同的错误

Cannot connect to the Docker daemon at tcp://localhost:2375/. Is the docker daemon running

我已尝试解决此问题。请遵循下面的,
1.启用TLS
使用.gitlab-ci.yml和config.toml在Runner中启用TLS。
我的.gitlab-ci.yml

image: docker:19.03
variables:
  DOCKER_HOST: tcp://localhost:2375/
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: "/certs"
  IMAGE_NAME: image_name

services:
  - docker:19.03-dind

stages:
  - build

publish:
  stage: build
  script:
    - docker build -t$IMAGE_NAME:$(echo $CI_COMMIT_SHA | cut -c1-10) .
    - docker push $IMAGE_NAME:$(echo $CI_COMMIT_SHA | cut -c1-10)
  only:
    - master

这是我的config.toml

[[runners]]
  name = MY_RUNNER
  url = MY_HOST
  token = MY_TOKEN_RUNNER
  executor = "docker"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "docker:stable"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/certs/client", "/cache"]                      
    shm_size = 0

1.禁用TLS
.gitlab-ci.yml

image: docker:18.09
variables:
  DOCKER_HOST: tcp://localhost:2375/
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  IMAGE_NAME: image_name

services:
  - docker:18.09-dind

stages:
  - build

publish:
  stage: build
  script:
    - docker build -t$IMAGE_NAME:$(echo $CI_COMMIT_SHA | cut -c1-10) .
    - docker push $IMAGE_NAME:$(echo $CI_COMMIT_SHA | cut -c1-10)
  only:
    - master

这是我的config.toml

[[runners]]
  environment = ["DOCKER_TLS_CERTDIR="]

有人知道吗?

    • 解决方案**

你可以看到在接受的答案.此外,在我的情况和另一个.看起来像是根本原因它从Linux服务器GitLab托管没有权限连接Docker.让我们检查GitLab和Docker之间的权限连接在您的服务器上.

mgdq6dx1

mgdq6dx11#

您希望将DOCKER_HOST设置为tcp://docker:2375。它是一个“服务”,即在一个单独的容器中运行,默认情况下以映像名称命名,而不是localhost。
下面是一个应该可以工作的.gitlab-ci.yml代码片段:

# Build and push the Docker image off of merges to master; based off
# of Gitlab CI support in https://pythonspeed.com/products/pythoncontainer/
docker-build:
  stage: build

  image:
    # An alpine-based image with the `docker` CLI installed.
    name: docker:stable

  # This will run a Docker daemon in a container (Docker-In-Docker), which will
  # be available at thedockerhost:2375. If you make e.g. port 5000 public in Docker
  # (`docker run -p 5000:5000 yourimage`) it will be exposed at thedockerhost:5000.
  services:
   - name: docker:dind
     alias: thedockerhost

  variables:
    # Tell docker CLI how to talk to Docker daemon; see
    # https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor
    DOCKER_HOST: tcp://thedockerhost:2375/
    # Use the overlayfs driver for improved performance:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""

  script:
    # Download bash:
    - apk add --no-cache bash python3
    # GitLab has a built-in Docker image registry, whose parameters are set automatically.
    # See https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#using-the-gitlab-contai
    #
    # CHANGEME: You can use some other Docker registry though by changing the
    # login and image name.
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
    - docker build -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"

  # Only build off of master branch:
  only:
    - master
eit6fx6z

eit6fx6z2#

您可以尝试禁用tls

services:
- name: docker:dind
  entrypoint: ["dockerd-entrypoint.sh", "--tls=false"]
script:
- export DOCKER_HOST=tcp://127.0.0.1:2375 && docker build --pull -t ${CI_REGISTRY_IMAGE} .

因为有一个有趣的阅读https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27300
docker:如果没有显式禁用TLS,dindv20将休眠16秒,这将导致构建容器比dockerd容器早启动的竞态条件

bfrts1fy

bfrts1fy3#

尝试使用这个.gitlab-ci.yml文件。当我指定DOCKER_HOST时,它对我有效

docker-build:
  stage: build

  image:
    # An alpine-based image with the `docker` CLI installed.
    name: docker:stable

  # This will run a Docker daemon in a container (Docker-In-Docker), which will
  # be available at thedockerhost:2375. If you make e.g. port 5000 public in Docker
  # (`docker run -p 5000:5000 yourimage`) it will be exposed at thedockerhost:5000.
  services:
   - name: docker:dind
     alias: thedockerhost

  variables:
    DOCKER_HOST: tcp://thedockerhost:2375/
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""

  script:
    # Download bash:
    - apk add --no-cache bash python3
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
    - docker build -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"

  only:
    - master
au9on6nz

au9on6nz4#

对我来说,接受的答案不起作用,而是我为运行者配置了TLS证书卷

[[runners]]
...
  [runners.docker]
    ...
    volumes = ["/certs/client", "/cache"]

并在我的.gitlab-ci.yaml中添加了证书目录的变量

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: "/certs"

根据该条:https://about.gitlab.com/blog/2019/07/31/docker-in-docker-with-docker-19-dot-03/#configure-tls
还有这个https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#docker-in-docker-with-tls-enabled-in-the-docker-executor

hyrbngr7

hyrbngr75#

你可以从.gitlab-ci文件中删除DOCKER_HOST,这个方法很有效。

相关问题