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

标签 docker docker-compose gitlab gitlab-ci gitlab-ci-runner

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

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

所以我的问题是:

  • 我能否在一台服务器上为我的所有存储库运行多个(2 或 3 个)gitlab-runner?
  • 我需要一个特定的或共享的运行者吗?到底有什么区别?
  • 为什么所有的教程都使用自托管的 Gitlab 实例而不是只使用 gitlab 存储库(是否不能将 gitlab-runner 与 gitlab.com 存储库一起使用?)
  • 是否可以在 gitlab-runner 管道中使用 docker-compose 并一次构建所有内容?
  • 最佳答案

    首先,您显然可以在 https://gitlab.com 上使用 GitLab CI/CD 功能。以及自托管的 GitLab 实例。除了您将在其上注册运行者的主机外,它不会改变任何内容:

  • https://gitlab.com/如果您使用 GitLab 而不托管它
  • https://your-custom-domain/如果您托管自己的 GitLab 实例

  • 您可以根据需要添加任意数量的运行者(我认为是这样,至少我每个项目有 5-6 个运行者没有问题)。你只需要为你的项目注册每个运行者。见 Registering Runners为了那个原因。

    至于共享运行者与特定运行者,我认为如果你想尝试 GitLab CI/CD,你应该坚持分享运行者。

    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.

    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.



    您可以在任何机器上安装自己的运行者,例如您的笔记本电脑。您可以 deploy it with Docker快速开始。

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

    What is Docker in Docker?

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



    这是一个示例用法,没有 docker-compose尽管:
    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 中使用 docker-compose宣言

    我的 Git 存储库如下所示:
    /my_repo
    |---- .gitignore    
    |---- .gitlab-ci.yml    
    |---- Dockerfile    
    |---- README.md
    

    我的运行者的 config.toml 看起来像:
    [[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 配置的更多信息。

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



    我希望它能回答你的问题

    关于docker - 如何使用 Gitlab CI 构建、推送和拉取多个 docker 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50683869/

    相关文章:

    continuous-integration - 支持 GitLab 中自动生成的 "Release notes"

    gitlab - 如何在Gitlab CI中获得提交差异?

    docker - Selenium.WebDriverException:消息:未知错误:net::ERR_NAME_NOT_RESOLVED

    docker - 使用 Postgresql 数据库在 docker 容器中运行简单的 Kotlin Ktor 应用程序

    postgresql - 如何使postgres在容器的新暴露端口(不是5432)上监听?

    python - 启动 docker 服务时运行多个命令

    GitLab CI pipeline failing with 您必须将 Bundler 2 或更高版本与此锁定文件一起使用

    database - 在 Docker 中运行 Postgres 时设置酒保备份

    containers - 为什么Docker(基于容器的技术)有用

    postgresql - Docker,在包含 "ubuntu:14.04"的容器中安装 postgresql,离开容器时丢失