docker-compose - gitlab runner - 如何缓存图像而不是每次都构建?
问题描述
我已经设置了 gitlab runner 来利用docker/compose:latest
图像来运行一组服务。
通过设置图像被缓存而不是每次都被拉取 pull_policy="if-not-present"
。config.toml
docker/compose:latest
如何将测试阶段使用的 docker 服务设置为使用缓存的图像(即开始使用的图像docker-compose up -d
)?我对这项dind
服务有点困惑。
.gitlab-ci.yml
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
DOCKER_DRIVER: overlay2
CI_REGISTRY: my-docker-registry.local:5000
image:
name: docker/compose:latest
services:
- name: docker:dind
command: ["--insecure-registry=my-docker-registry.local:5000"]
test:
stage: test
script:
- docker-compose up -d
/etc/gitlab-runner/config.toml
如下:
concurrent = 1
check_interval = 0
[[runners]]
name = "user1"
url = "http://gitlab.example.local/"
token = "<MYTOKEN>"
executor = "docker"
[runners.docker]
extra_hosts = ["my-docker-registry.local:192.168.1.100"]
tls_verify = false
tls_verify = false
image = "docker:latest"
privileged = true
disable_cache = false
pull_policy="if-not-present"
volumes = ["/certs/client", "/cache"]
disable_entrypoint_overwrite = false
oom_kill_disable = false
shm_size = 0
[runners.cache]
解决方案
仅适用于跑步者自己拉取pull_policy
的图像。image:
您在键(或services[]:name
)中设置的 IE 图像。
如果您在使用docker
docker执行器时在作业中使用(例如,在您使用docker-compose up -d
的地方),那么您将无法利用缓存层,并且必须先从注册表中提取图像或从头开始构建。
原因是 docker-in-docker GitLab 作业使用该docker:dind
服务作为其 docker 守护进程。该服务每次都会提供一个新的状态,因此不会利用缓存的图像,即使运行器本身已经缓存了图像。
但是,您可以从注册表中提取图像,并--cache-from
使用docker build
.
script:
- docker pull $CI_REGISTRY_IMAGE:latest || true
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
当然,您仍然必须每次都拉取镜像,但这可能比构建更快,特别是如果您的镜像安装了许多依赖项或编译大型模块或类似的东西。
有关更多信息,请参阅文档中的docker-in-docker部分。
推荐阅读
- python-3.x - leetcode 88 合并排序数组 python3 切片问题
- swift - 通过对象的枚举属性使用 NSPredicate 获取核心数据托管对象的问题
- c# - Moq 成员变量不会因设置无效而引发错误
- c# - RESTful API - 为每个实体创建“服务”和“接口”?
- r - 创建一个函数,不小心覆盖了输出
- bash - bash 变量替换和引用
- android - MaterialComponents 主题中断 TabLayout
- python - 如何在 python 中解析包含 HTML 代码的 XLS 文件?
- c# - .net 核心复选框模型绑定视图
- r - 在 R 中使用 ggtext 和 gridtext 在 R 中具有跨越图例的多面板图