docker - gitlab-ci.yml : how to create a docker image using dind

标签 docker gitlab gitlab-ci

我的 GitLab-ci.yml 中有以下逻辑:

stages:
  - build
  - deploy

variables:
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG

make_patch:
  image: node:10.19
  stage: build
  script:
    - npm install
    - make
    - make source-package
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
    - node_modules/
  artifacts:
    when:
    paths:
      - test.tar.bz2
    expire_in: 2 days

  test_feature:
    stage: deploy
    image: dockerhub_image:123
    script:
      - apt-get install bzip2
      - apt-get install curl -y
      - ls -lah
      - curl -kL https://mygitlabserver/namespace/project/-/jobs/artifacts/master/download?job=make_patch
      - tar -xvjf test.tar.bz2
      - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
      - docker build --no-cache -t $IMAGE_TAG .
      - docker push $IMAGE_TAG
所以我试图通过构建一个nodejs图像来测试我的repo中的变化;如果它可以编译,那么我会保存一个 zip 文件,其中包含在潜在生产系统中需要使用的所有 Web 文件。
然后我从公共(public) docker hub 下载现有图像(因为这实际上是我们正在修改的开源应用程序)。我想在那里更新 web 文件夹,然后尝试从中创建一个 docker 容器并将其保存到我的项目存储库中。
正如您从下面的输出中看到的那样,它目前正在失败,因为正如@marvin 指出的那样, dockerhub_image:123 in 没有 docker cli。
523$ docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
524/bin/bash: line 107: docker: command not found
525[cmd] sh exited 1
526[cont-finish.d] executing container finish scripts...
527[cont-finish.d] done.
528[s6-finish] waiting for services.
529[s6-finish] sending all processes the TERM signal.
530[s6-finish] sending all processes the KILL signal and exiting.
532ERROR: Job failed: exit code 1
我在我的项目中定义了一个 Deploy token ,如下所示:
Name: webtoken
Username: webtoken-1
Created: Aug 6, 2020
Expires: Never
Scopes
read_repository, read_registry, write_registry, read_package_registry, write_package_registry
我找到了这篇文章:Gitlab CI - docker: command not found
但是我很难理解在我的案例中我会在哪里包含对 docker 图像的引用。或者我可以在这个“test_feature”工作中添加一个服务条款吗?
services:
  - docker:dind
对不起,补救问题。我对 docker 和 GitLab 都是新手。
我尝试像这样更改 test_feature 作业,但它仍然失败并出现相同的错误:
  test_feature:
    stage: deploy
    image: dockerhub_image:123
    services:
      - docker:dind
    script:
      - apt-get install bzip2
      - apt-get install curl -y
      - ls -lah
      - curl -kL https://mygitlabserver/namespace/project/-/jobs/artifacts/master/download?job=make_patch
      - tar -xvjf test.tar.bz2
      - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
      - docker build --no-cache -t $IMAGE_TAG .
      - docker push $IMAGE_TAG

最佳答案

要扩展@Alessandro Chiitolina 的答案:
首先,如果您想查看在 GitLab CI 中使用 docker-in-docker 方法构建 Docker 的整个工作示例,可以使用 here .
其次,如果我不知道如何在 GitLab CI 中做某事,我发现检查 Auto DevOps 很有用。模板。使用 docker-in-docker 构建 Docker 可用 here .您甚至可以按照 here 的描述直接将其导入到您的管道中。 .
最后,您也可以使用 kaniko 来代替 docker-in-docker 方法,如 here 所述.
附带说明 - 无需从 make_patch 手动下载工件在 test_feature作业 - 将自动下载 - 见 here详情。

关于docker - gitlab-ci.yml : how to create a docker image using dind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63286251/

相关文章:

Docker 服务不会在容器内运行 - 错误 : ulimit: error setting limit (Operation not permitted)

azure - 尝试让 Azure Kubernetes 服务使用服务中的集群负载均衡器时出错

Azure函数应用程序: what's the feature to publish a Docker container about?

docker - gitlab CI + Docker + .NET Core,我不明白为什么只安装较旧的.net版本

gitlab - 根据脚本命令结果将 GitLab CI 作业添加到管道

docker - 无法从 gcloud 计算虚拟机访问私有(private)容器注册表

Git: git lfs 迁移后的 "The system cannot find the path specified."

intellij-idea - 如何将项目从 IDEA 连接到 Gitlab

docker - Gitlab CI - docker : command not found

git - 你如何使用 gitlab-ci 作业推送到 gitlab 仓库?