GitLab Runner Docker执行器中的缓存层-长时间DinD容器

我正在我的项目中使用GitLab CI,我创建了一个图像来进行测试和构建。当我在docker executor中运行它时,每个工作都需要从头开始下载映像。我需要缓存图层和提取图像以改善构建和部署时间(5分钟,使用不安全选项最多可以达到1分钟)。

我搜索了多个链接和多个文章,其中许多人遇到相同的问题。但是,GitLab团队无法解决问题。而且社区没有一个可靠的解决方案。以下链接具有相同的问题:

  1. 最佳答案无效:Store layers in gitlab ci docker executor
  2. 进行了多项更改以绕过此问题,但无济于事:https://blog.scottlogic.com/2018/02/09/multi-dind-ci-boxes.html
  3. 讨论不要使用已安装的 docker.sock https://gitlab.com/gitlab-org/gitlab-foss/issues/17769
  4. 讨论已安装的 docker.sock https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
  5. 建立一个长时间的容器(不要与我合作):https://medium.com/@tonywooster/docker-in-docker-in-gitlab-runners-220caeb708ca
  6. 未安装的文档 docker.sock https://docs.gitlab.com/ce/ci/docker/using_docker_build.html#use-docker-in-docker-executor
  7. 音量配置示例:https://github.com/ayufan/gitlab-ci-multi-runner/blob/master/docs/configuration/advanced-configuration.md#the-runnersdocker-section

最可能的方法(带有层缓存)是使用单独的容器,使运行程序连接到它,并从中触发执行。这样,所有层将位于“无限生命”容器中,并且在阶段结束时不会丢失所有缓存。 考虑将 docker.sock 公开为挂载的方法不仅不安全,而且在容器之间共享文件还存在许多问题,因为它们都是兄弟姐妹,而不是共享卷的父母和孩子。

使用无限生命容器的方法如下所示:

docker run --privileged --name gitlab-dind -d --restart=always  docker:19-dind --storage-driver=overlay2

docker network create gitlab-runner-net

docker run --privileged --name gitlab-runner-dind --network gitlab-runner-net --publish=2375:2375 --publish=2376:2376 -d docker:19-dind --storage-driver=overlay2

然后按如下所示修改 config.toml

[runners.docker]
  tls_verify = false
  image = "docker:19"   <--------
  privileged = false     <--------
  disable_cache = false
  volumes = ["/cache"]
  links = ["gitlab-runner-dind:docker"]   <-----------
  shm_size = 0
[runners.cache]

[runners.docker]
  host = "tcp://gitlab-runner-dind:2375"    <--------
  tls_verify = false
  image = "docker:19"   <--------
  privileged = true     <--------
  disable_cache = false
  volumes = ["/cache"]
  network_mode = "gitlab-runner-net"   <-----------
  shm_size = 0
[runners.cache]

我也尝试过使用环境变量(在 config.toml .gitlab-ci.yml 上):

DOCKER_TLS_CERTDIR=""
DOCKER_HOST=tcp://gitlab-runner-dind:2375

并从 .gitlab-ci.yml 中删除:

services:
  - docker:19-dind
  alias: docker

我当前的结果是:

Running with gitlab-runner 12.4.1 (HASH)
  on NAME_OF_MY_RUNNER HASH
ERROR: Preparation failed: error during connect: Get http://gitlab-runner-dind:2375/v1.25/info: dial tcp: lookup gitlab-runner-dind on 172.31.0.2:53: no such host (executor_docker.go:980:0s)
Will be retried in 3s ...
ERROR: Preparation failed: error during connect: Get http://gitlab-runner-dind:2375/v1.25/info: dial tcp: lookup gitlab-runner-dind on 172.31.0.2:53: no such host (executor_docker.go:980:0s)
Will be retried in 3s ...
ERROR: Preparation failed: error during connect: Get http://gitlab-runner-dind:2375/v1.25/info: dial tcp: lookup gitlab-runner-dind on 172.31.0.2:53: no such host (executor_docker.go:980:0s)
Will be retried in 3s ...
ERROR: Job failed (system failure): error during connect: Get http://gitlab-runner-dind:2375/v1.25/info: dial tcp: lookup gitlab-runner-dind on 172.31.0.2:53: no such host (executor_docker.go:980:0s)

使用已安装的 docker.sock 可以正常工作。但这是不安全的,并且卷在共享文件,工件和缓存方面存在许多问题。

root@GitlabRunner:/etc/gitlab-runner# gitlab-runner --version
Version:      12.4.1
Git revision: 05161b14
Git branch:   12-4-stable
GO version:   go1.10.8
Built:        2019-10-28T12:49:57+0000
OS/Arch:      linux/amd64
chen192 回答:GitLab Runner Docker执行器中的缓存层-长时间DinD容器

使用kaniko可能更好。当您使用dind进行构建时,效果不好。 (danger note @ gitlab reference

kaniko提供了使用存储库中的缓存机制的机会。 kaniko@github

您唯一需要的是某个地方的存储库(我建议使用Artifactory)。使用Artifactory,您还可以通过dind进行缓存(请参见here)。

本文链接:https://www.f2er.com/3142491.html

大家都在问