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

时间:2020-04-08 16:21:15

标签: docker gitlab gitlab-ci gitlab-ci-runner

我正在尝试在GitLab中构建CI管道。我想问一下有关docker在GitLab CI中的工作。

此问题:https://gitlab.com/gitlab-org/gitlab-runner/issues/4501#note_195033385

两种方法都遵循指示。使用TLS而未使用TLS。 但是它仍然被卡住。哪个错误

无法通过tcp:// localhost:2375 /连接到Docker守护程序。 Docker守护程序是否正在运行

我已经尝试解决此问题。跟在下面,

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

2)禁用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="]

有人知道吗?

1 个答案:

答案 0 :(得分:2)

您要将DOCKER_HOST设置为tcp://docker:2375。这是一项“服务”,即默认运行在单独的容器中,该容器以映像名称命名,而不是本地主机。

下面的.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