如何使用Gitlab CI构建,推送和拉取多个Docker容器? [英] How to build, push and pull multiple docker containers with Gitlab CI?

查看:156
本文介绍了如何使用Gitlab CI构建,推送和拉取多个Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docker-compose文件,该文件构建了两个容器,一个节点应用程序和一个ngnix服务器。现在,我想在Gitlab运行程序的帮助下在服务器上自动化构建和运行过程。我对与CI有关的东西还很陌生,所以请原谅我的方法:

I have a docker-compose file which builds two containers, a node app and a ngnix server. Now I would like to automate the build and run process on the server with the help of Gitlab runners. I am pretty new to CI-related stuff so please excuse my approach:

我想在gitlab.com上创建多个存储库,并为每个存储库创建一个Dockerfile 。我现在是否必须将gitlab-runner实例与这些项目中的每一个相关联才能构建映像,将其推送到docker repo并让服务器从那里拉出它?然后我不得不以某种方式将docker-compose文件推送到服务器上,然后从那里组成所有内容。

I would want to create multiple repositories on gitlab.com and have a Dockerfile for each one of these. Do I now have to associate a gitlab-runner instance with each of these projects in order to build the image, push it to a docker repo and let the server pull it from there? And then I would have to somehow push the docker-compose file on the server and compose everything from there.

所以我的问题是:


  1. 我可以在一台服务器上为所有存储库运行多个(2或3个)gitlab-runner吗?

  2. 我可以吗?需要一个特定的或共享的运行器,并且到底有什么区别?

  3. 为什么所有教程都使用自托管的Gitlab实例,而不是仅使用gitlab repos(无法将gitlab-runner与

  4. 是否可以在gitlab-runner管道中使用docker-compose并立即构建所有内容?

  1. Am I able to run multiple (2 or 3) gitlab-runner for all of my repos on one server?
  2. Do I need a specific or shared runner and what exactly is the difference?
  3. Why are all tutorials using self hosted Gitlab instances instead of just using gitlab repos (Is it not possible to use gitlab-runner with gitlab.com repos?)
  4. Is it possible to use docker-compose in a gitlab-runner pipeline and just build everything at once?


推荐答案

首先,您显然可以在 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驱动。自动缩放意味着减少了启动构建的等待时间,并为每个项目隔离了VM,从而最大限度地提高了安全性。

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.

它们可免费用于公共开源项目,并且仅限于私人项目每个小组每月2000 CI分钟。阅读有关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.

尽管您可以在任何机器上将自己的运行器安装在任何机器上,例如laptotp。您可以与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,则可以在 gitlab-ci.yml 文件中使用docker-compose 执行程序,并在您的服务器上安装 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注册表,但是您可以推送到任何注册表。当然,您可以随时在脚本声明

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

中使用docker-compose我的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/run ner / 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/

我希望它能回答您的问题

I hope it answers your questions

这篇关于如何使用Gitlab CI构建,推送和拉取多个Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆