简体   繁体   English

为 gitlab-ci 使用缓存 docker 图像

[英]Use cache docker image for gitlab-ci

I was wondering is it possible to use cached docker images in gitlab registry for gitlab-ci?我想知道是否可以在 gitlab-ci 的 gitlab 注册表中使用缓存的 docker 图像? for example, I want to use node:16.3.0-alpine docker image, can I cache it in my gitlab registry and pull it from that and speed up my gitlab ci instead of pulling it from docker hub? for example, I want to use node:16.3.0-alpine docker image, can I cache it in my gitlab registry and pull it from that and speed up my gitlab ci instead of pulling it from docker hub?

Yes, GitLab's dependency proxy features allow you to configure GitLab as a " pull through cache ".是的,GitLab 的依赖代理功能允许您将 GitLab 配置为“通过缓存拉取”。 This is also beneficial for working around rate limits of upstream sources like dockerhub.这也有利于解决 dockerhub 等上游资源的速率限制

It should be faster in most cases to use the dependency proxy, but not necessarily so.在大多数情况下,使用依赖代理应该更快,但不一定如此。 It's possible that dockerhub can be more performant than a small self-hosted server, for example.例如,dockerhub 可能比小型自托管服务器性能更高。 GitLab runners are also remote with respect to the registry and not necessarily any "closer" to the GitLab registry than any other registry over the internet. GitLab 跑步者相对于注册表也是远程的,并且不一定比 Internet 上的任何其他注册表更“接近” GitLab 注册表。 So, keep that in mind.所以记住这一点。

As a side note, the absolute fastest way to retrieve cached images is to self-host your GitLab runners and hold images directly on the host.附带说明一下,检索缓存图像的绝对最快方法是自行托管 GitLab 跑步者并将图像直接保存在主机上。 That way, when jobs start, if the image already exists on the host, the job will start immediately because it does not need to pull the image (depending on your pull configuration).这样,当作业启动时,如果主机上已经存在映像,作业将立即启动,因为它不需要拉取映像(取决于您的拉取配置)。 (that is, assuming you're using images in the image: declaration for your job) (也就是说,假设您在图像中使用image:您的工作声明)

I'm using a corporate Gitlab instance where for some reason the Dependency Proxy feature has been disabled.我正在使用公司 Gitlab 实例,由于某种原因,依赖代理功能已被禁用。 The other option you have is to create a new Docker image on your local machine, then push it into the Container Registry of your personal Gitlab project.另一个选择是在本地计算机上创建一个新的 Docker 映像,然后将其推送到您个人 Gitlab 项目的容器注册表中。

# First create a one-line Dockerfile containing "FROM node:16.3.0-alpine"
docker pull node:16.3.0-alpine
docker build . -t registry.example.com/group/project/image
docker login registry.example.com -u <username> -p <token>
docker push registry.example.com/group/project/image

where the image tag should be constructed based on the example given on your project's private Container Registry page.应根据项目私有 Container Registry 页面上给出的示例构建图像标签。

Now in your CI job, you just change image: node:16.3.0-alpine to image: registry.example.com/group/project/image .现在在您的 CI 工作中,您只需将image: node:16.3.0-alpine更改为image: registry.example.com/group/project/image You may have to run the docker login command (using a deploy token for credentials, see Settings -> Repository) in the before_script section -- I think maybe newer versions of Gitlab will have the runner authenticate to the private Container Registry using system credentials, but that could vary depending on how it's configured.您可能必须在before_script部分中运行docker login命令(使用部署令牌作为凭据,请参阅设置-> 存储库) - 我认为 Gitlab 的较新版本将让运行程序使用系统凭据对私有容器注册表进行身份验证,但这可能会因配置方式而异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM