首页 > 解决方案 > 为 Docker Dind 服务设置代理

问题描述

我正在使用 Gitlab CI/CD 管道,并且正在尝试使发布作业正常工作。
我的网络受公司政策保护,所以我需要使用代理。我为我的 Docker 设置了代理配置。它运行良好,直到docker build工作被执行。

我的发布工作如下所示:

  publish_test:
  stage: publish
  image: docker:19.03.1
  only:
    - test
  services:
    - docker:19.03.1-dind
  variables:
    IMAGE: ${IMAGE}
  before_script:
    - mkdir tmp
    - cp docker/Dockerfile build/libs/backend_v2-*.jar tmp
    - cd tmp
    - echo ${CI_REGISTRY_PASSWORD} | docker login ${CI_REGISTRY} -u ${CI_REGISTRY_USER} --password-stdin
  script:
    - docker pull ${IMAGE} || true
    - docker tag ${IMAGE} ${IMAGE}-backup || true
    - docker push ${IMAGE}-backup || true
    - echo $CI_REGISTRY
    - docker build -t ${IMAGE} .
    - docker push ${IMAGE}
  cache:
    key: ${CI_COMMIT_REF_NAME}
    policy: pull
    paths:
      - build/

问题出在这里:docker build -t ${IMAGE} .
由于连接错误,他无法拉取 Docker 映像:

 Step 1/4 : FROM azul/zulu-openjdk-alpine:11
 Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
 ERROR: Job failed: exit code 1

到目前为止,我已经尝试了多种解决方案,但没有一个对我有用:
1. 尝试将代理变量添加到 Gitlab 变量中并将代理配置放入这项工作中,如下所示:

- export http_proxy=${HTTP_PROXY};
- export https_proxy=${HTTP_PROXY};
- export no_proxy=${NO_PROXY};
  1. 尝试使用build-arg参数:

docker build --progress --build-arg ${HTTP_PROXY} --build-arg ${HTTPS_PROXY} --build-arg ${NO_PROXY} -t ${IMAGE} .

  1. 我在 Gitlab 文档中找到了我的案例 - https://docs.gitlab.com/runner/configuration/proxy.html#proxy-settings-when-using-dind-service。尝试按照那里的指示进行操作,但没有成功。

我怎样才能让它为我工作?我需要为此 Docker 构建作业设置代理设置,以便它可以提取必要的 Docker 映像。任何帮助表示赞赏。

标签: dockerproxycontinuous-integrationgitlabcontinuous-deployment

解决方案


  1. 您不需要重新导出配置为 GitLab 变量的环境。将脚本env | grep proxy或添加env | grep PROXY到您的管道以查看是否存在环境。
  2. 假设您的代理环境看起来像这个HTTP_PROXY=http://127.0.0.1:3001构建命令应该是docker build --progress --build-arg http_proxy=${HTTP_PROXY} ...

看起来你使用错误的构建参数--build-arg ${HTTP_PROXY}应该是--build-arg http_proxy=${HTTP_PROXY}和相同https_proxyno_proxy


推荐阅读