如何在 gitlab CI 中的 docker-in-docker (dind) 中设置代理
How to set proxy in docker-in-docker (dind) in gitlab CI
我正在尝试使用 gitlab CI 设置一个作业,以从 docker 文件构建一个 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 search
、docker info
和 docker 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 模式下使用了 runner,as the documentation instructs to do。
我该如何解决这个问题?
奇怪的是,解决方案是使用 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。
我无法让 docker-in-docker (dind) 在我们的公司代理后面工作。
特别是,即使遵循 the instructions here,docker build
命令在执行 FROM <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
如果你想在代理后面的 gitlab CI 中使用 docker-in-docker (dind),你还需要在你的 gitlab-ci 中设置 no_proxy 变量.yml 文件。 NO_PROXY 主机“docker”。
这是适用于我的 dind 的 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
祝你好运!
我正在尝试使用 gitlab CI 设置一个作业,以从 docker 文件构建一个 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 search
、docker info
和 docker 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 模式下使用了 runner,as the documentation instructs to do。
我该如何解决这个问题?
奇怪的是,解决方案是使用 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。
我无法让 docker-in-docker (dind) 在我们的公司代理后面工作。
特别是,即使遵循 the instructions here,docker build
命令在执行 FROM <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
如果你想在代理后面的 gitlab CI 中使用 docker-in-docker (dind),你还需要在你的 gitlab-ci 中设置 no_proxy 变量.yml 文件。 NO_PROXY 主机“docker”。
这是适用于我的 dind 的 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
祝你好运!