在 docker 运行ner 上构建并 运行 图像,无需推送它

Build and run an image on a docker runner, without pushing it

我有一个 CI 运行 类型 docker 的 Gitlab,以及一个带有 Dockerfile 的 repo,我将使用它来构建我的工件。

我知道我可以分两个阶段完成,一个阶段使用 docker 图像构建图像,将其推送到注册表,另一个阶段使用图像构建工件。

但我不关心将图像保存在注册表中,我想跳过这一步,直接在我的管道中使用 docker 图像而不推送它。

所以我在 .gitlab-ci.yml 中尝试了以下方法,只是 docker builddocker run

build-docs:
  tags:
    - docker
  image: docker:stable
  stage: build-docs
  script:
    - docker build -t $IMAGE_TAG .
    - docker run --rm $IMAGE_TAG source build
  artifacts:
    paths:
      - build

我的 Dockerfile 有一个入口点,它采用源目录和构建目录,当然我的存储库的源目录中充满了文件

然而,docker run步骤没有找到存储库文件,好像source目录是空的,但我猜这是因为运行宁一个docker docker 图像中的图像有点奇怪。

如何修复我的 运行 步骤以便找到文件,或者是否有不同的方法来执行我想要的操作?

您必须 运行 您的作业中 Docker 容器,其中装载作业的当前目录,如 Gitlab DinD limitations doc 中所述。例如你可以这样做:

script:
  - docker build -t $IMAGE_TAG .
  # assuming 'source' folder exists at 'path/to/source' in your Git repo
  - docker run -v "/builds/$CI_PROJECT_PATH/path/to/source:/path/to/source" $IMAGE_TAG /path/to/source build

在 Docker 中使用 Docker,然后您的 $IMAGE_TAG 容器应该有可用的本地构建文件(包括 source 文件夹)