docker - gitlab-ci.yml:如何使用 dind 创建 docker 镜像
问题描述
我的 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 文件。然后我从公共 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 令牌,如下所示:
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 的整个工作示例,请点击此处。
Secondly, if I don't know how to do something in GitLab CI, I find it usefult to check Auto DevOps templates. The one for Docker build using docker-in-docker is available here. You can even import it to your pipeline directly as described here.
Finally, instead of docker-in-docker approach, you can also use kaniko, as described here.
As a side note - there's no need to manually download the artifact from make_patch
in test_feature
job - it will be downloaded automatically - see here for details.
推荐阅读
- javascript - 单击不保留按钮(取消按钮)时,Vue.js如何保留待办事项?
- postgresql - 类型“MAX”不存在
- python - 是什么导致 Python 错误“错误转义\C”?
- java - 如何跳过 Spring Kafka Consumer 中的损坏(不可序列化)消息?
- python - 在 Python 中为数字矩阵创建运行长度
- c - c的自定义malloc函数
- google-apps-script - 如何从谷歌应用脚本调用 Python 脚本
- javascript - 如何通过 ip 查找服务向非欧盟客户显示弹出窗口
- .net - 在没有控制台的情况下启动 Windows 可执行文件?
- json - 为什么 Python 3 json.loads("foo.com") 还不能工作 json.loads("123.6") 呢?