如何在gitlab CI

时间:2018-08-08 20:39:24

标签: docker proxy gitlab gitlab-ci docker-in-docker

我正在尝试使用gitlab CI设置作业,以从dockerfile构建docker映像,但是我在代理后面。

我的.gitlab-ci.yml如下:

image: docker:stable

variables:
  DOCKER_HOST: tcp://docker:2375
  DOCKER_DRIVER: overlay2
  HTTP_PROXY: $http_proxy
  HTTPS_PROXY: $http_proxy
  http_proxy: $http_proxy
  https_proxy: $http_proxy

services:
  - docker:dind

before_script:
  - wget -O - www.google.com # just to test
  - docker search node # just to test
  - docker info # just to test

build:
  stage: build
  script:
    - docker build -t my-docker-image .

wget有效,从理论上讲,这意味着代理设置是正确的

但是命令docker searchdocker infodocker build不起作用,显然是因为代理问题。

作业输出的摘录:

$ docker search node
Warning: failed to get default registry endpoint from daemon (Error response from  daemon:
    [and here comes a huge raw HTML output including the following message: "504 - server did not respond to proxy"]

看来docker没有从环境变量中读取设置代理。

注意:我确实在--privileged模式as the documentation instructs to do中使用跑步者。

我该如何解决?

3 个答案:

答案 0 :(得分:1)

我无法让docker-in-docker(dind)在我们的公司代理后面工作。

尤其是,即使执行docker build时,即使遵循the instructions hereFROM <some_image>命令仍然会失败,因为它无法下载图像。

我使用kaniko获得了更大的成功,这似乎是Gitlabs当前推荐的进行Docker构建的建议。

.NET Core项目的简单构建脚本如下:

build:
  stage: build
  image: $BUILD_IMAGE
  script:
    - dotnet build 
    - dotnet publish Console--output publish
  artifacts:
    # Upload all build artifacts to make them available for the deploy stage.
    when: always
    paths:
      - "publish/*"
    expire_in: 1 week

kaniko:
  stage: dockerise
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    # Construct a docker-file
    - echo "FROM $RUNTIME_IMAGE" > Dockerfile
    - echo "WORKDIR /app" >> Dockerfile
    - echo "COPY /publish ." >> Dockerfile
    - echo "CMD [\"dotnet\", \"Console.dll\"]" >> Dockerfile

    # Authenticate against the Gitlab Docker repository.
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json

    # Run kaniko
    - /kaniko/executor --context . --dockerfile Dockerfile --destination $CI_REGISTRY_IMAGE:$VersionSuffix

答案 1 :(得分:1)

如果您希望能够在代理后面的gitlab CI中使用docker-in-docker(dind),则还需要在gitlab-ci.yml文件中设置no_proxy变量。主机“ docker”为NO_PROXY。

这是与我的日记配合使用的gitlab-ci.yml:

image: docker:19.03.12

variables:
  DOCKER_TLS_CERTDIR: "/certs"
  HTTPS_PROXY: "http://my_proxy:3128"
  HTTP_PROXY: "http://my_proxy:3128"
  NO_PROXY: "docker"

services:
  - docker:19.03.12-dind
  
before_script:
  - docker info

build:
  stage: build
  script:
    - docker run  hello-world

祝你好运!

答案 2 :(得分:0)

奇怪的是,解决方案是改用gitlab提供的特殊Dind(docker-in-docker)映像,它无需设置服务和其他任何东西即可工作。有效的.gitlab-ci.yml如下:

image: gitlab/dind:latest

before_script:
  - wget -O - www.google.com
  - docker search node
  - docker info

build:
  stage: build
  script:
    - docker build -t my-docker-image .

别忘了gitlab-runner must be registered with the --privileged flag