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

问题描述 投票:2回答:1

我正在我的项目中使用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.sockhttps://gitlab.com/gitlab-org/gitlab-foss/issues/17769
  4. 讨论使用的已安装docker.sockhttps://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.sockhttps://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
docker gitlab gitlab-ci gitlab-ci-runner dind
1个回答
1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.