且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Gitlab CI 构建、推送和拉取多个 docker 容器?

更新时间:2023-11-22 22:36:16

首先,您显然可以在 https://gitlab.com 以及自托管的 GitLab 实例.除了您将在其上注册跑步者的主机外,它不会改变任何内容:

First of all, you can obviously use GitLab CI/CD features on https://gitlab.com as well as on self hosted GitLab instances. It doesn't change anything, except the host on which you will register your runner:

  • https://gitlab.com/ in case you uses GitLab without hosting it
  • https://your-custom-domain/ in case you host your own instance of GitLab

您可以根据需要添加任意数量的跑步者(我认为是这样,至少我每个项目有 5-6 名跑步者没有问题).你只需要为你的项目注册每个跑步者.请参阅注册跑步者.

You can add as many runners as you want (I think so, and at least I have 5-6 runners per project without problem). You just need to register each of those runners for your project. See Registering Runners for that.

对于共享运行器与特定运行器,我认为如果您想尝试 GitLab CI/CD,我认为您应该坚持使用共享运行器.

As for shared runners versus specific runner, I think you should stick to share runners if you wish to try GitLab CI/CD.

GitLab.com 上的 Shared Runners 在自动缩放模式下运行,并由 DigitalOcean 提供支持.自动缩放意味着缩短启动构建的等待时间,并为每个项目隔离虚拟机,从而最大限度地提高安全性.

Shared Runners on GitLab.com run in autoscale mode and are powered by DigitalOcean. Autoscaling means reduced wait times to spin up builds, and isolated VMs for each project, thus maximizing security.

它们可以免费用于公共开源项目,对于私人项目,每个组每个月的 CI 分钟限制为 2000 分钟.阅读所有 GitLab.com 计划.

They're free to use for public open source projects and limited to 2000 CI minutes per month per group for private projects. Read about all GitLab.com plans.

您可以在任何机器上安装自己的跑步者,例如您的笔记本电脑.您可以将其与 Docker 一起部署以快速开始.

You can install your own runners on literraly any machine though, for example your laptotp. You can deploy it with Docker for a quick start.

最后,是的,如果您使用 ssh 执行程序并具有 docker-compose 文件,您可以在 gitlab-ci.yml 文件中使用 docker-compose> 安装在您的服务器上.但我建议使用 docker 执行器并使用 docker:dind(Docker 中的 Docker)镜像

Finally, yes you can use docker-compose in a gitlab-ci.yml file if you use ssh executor and have docker-compose install on your server. But I recommend using the docker executor and use docker:dind (Docker in Docker) image

Docker 中的 Docker 是什么?

What is Docker in Docker?

虽然通常不建议在 Docker 内部运行 Docker,但有一些合法的用例,例如 Docker 本身的开发.

Although running Docker inside Docker is generally not recommended, there are > some legitimate use cases, such as development of Docker itself.

这是一个示例用法,虽然没有 docker-compose:

Here is an example usage, without docker-compose though:

image: docker:latest

services:
  - name: docker:dind
    command: ["--experimental"]


before_script:
  - apk add --no-cache py-pip      # <-- add python package install pip
  - pip install docker-compose     # <--- add docker-compose 
  - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin    # <---- Login to your registry

build-master:
  stage: build
  script:
    - docker build --squash --pull -t "$CI_REGISTRY_USER"/"$CI_REGISTRY_IMAGE":latest .
    - docker push "$CI_REGISTRY_USER"/"$CI_REGISTRY_IMAGE":latest
  only:
    - master

build-dev:
  stage: build
  script:
    - docker build --squash --pull -t "$CI_REGISTRY_USER"/"$CI_REGISTRY_IMAGE":"$CI_COMMIT_REF_SLUG" .
    - docker push "$CI_REGISTRY_USER"/"$CI_REGISTRY_IMAGE":"$CI_COMMIT_REF_SLUG"
  except:
    - master

如您所见,我构建了 Docker 映像,对其进行了标记,然后将其推送到我的 Docker 注册表,但您可以推送到任何注册表.当然,您可以随时在 script 声明

As you can see, I build the Docker image, tag it, then push it to my Docker registry, but you could push to any registry. And of course you could use docker-compose at any time in a script declaration

我的 Git 存储库看起来像:

My Git repository looks like :

/my_repo
|---- .gitignore    
|---- .gitlab-ci.yml    
|---- Dockerfile    
|---- README.md

我的跑步者的 config.toml 看起来像:

And the config.toml of my runner looks like:

[[runners]]
  name = "4Gb digital ocean vps"
  url = "https://gitlab.com"
  token = "efnrong44d77a5d40f74fc2ba84d8"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:dind"
    privileged = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
    shm_size = 0
  [runners.cache]

你可以看看https://docs.gitlab.com/runner/configuration/advanced-configuration.html 有关 Runner 配置的更多信息.

You can take a look at https://docs.gitlab.com/runner/configuration/advanced-configuration.html for more information about Runner configuration.

注意:这里使用的所有变量都是秘密变量.请参阅 https://docs.gitlab.com/ee/ci/variables/供解释

Note : All the variables used here are secret variables. See https://docs.gitlab.com/ee/ci/variables/ for explanations

希望它能回答你的问题