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

▼魔方 西西 提交于 2020-07-23 08:09:21

问题


I'm trying to build the CI pipeline in GitLab. I'd like to ask about the docker for work in GitLab CI.

from this issue : https://gitlab.com/gitlab-org/gitlab-runner/issues/4501#note_195033385

I'm follow the instruction for both ways. With TLS and not used TLS. But It's still stuck. Which in same error

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

I've try to troubleshooting this problem. follow by below,

1) enable TLS

Which used .gitlab-ci.yml and config.toml for enable TLS in Runner.

This my .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   

And this my 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

2) disable 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   

And this my config.toml

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

Anyone have idea?


回答1:


You want to set DOCKER_HOST to tcp://docker:2375. It's a "service", i.e. running in a separate container, by default named after the image name, rather than localhost.

Here's a .gitlab-ci.yml snippet that should work:

# 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



来源:https://stackoverflow.com/questions/61105333/cannot-connect-to-the-docker-daemon-at-tcp-localhost2375-is-the-docker-daem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!