Docker 标签在 Bitbucket 管道中的步骤之间丢失

Docker tags are lost between steps in Bitbucket pipelines

我正在使用 Bitbucket pipelines 使用 Gradle 构建 Docker 图像。这是我的构建:

definitions:
  steps:
    - step: &build-docker
        name: Build Docker images
        image:
          name: openjdk:8
        services:
          - docker
        script:
          - ./gradlew dockerBuildImage
          - docker image ls
        caches:
          - gradle-wrapper
          - gradle
          - docker
    - step: &publish-docker
        name: Publish Docker images
        image:
          name: docker
        services:
          - docker
        script:
          - docker image ls
        caches:
          - docker
pipelines:
  default:
    - step: *build-docker
    - step: *publish-docker

我的 build.gradle.kts 配置为使用 UTC 时间戳标记图像:

configure<DockerExtension> {
    configure(this.getProperty("javaApplication"), closureOf<DockerJavaApplication> {
        baseImage = "openjdk:8-jre-alpine"
        tag = "${name}:${Instant.now().epochSecond}"
    })
}

当我 运行 dockerBuildImage 在本地执行任务时,我可以看到我标记的图像:

$docker image ls
REPOSITORY     TAG          IMAGE ID       CREATED       SIZE
…
forklift-1     1540454741   93fd78260bd1   5 weeks ago   105MB
forklift-2     1540454741   3c8e4e191fd3   5 weeks ago   105MB
forklift-3     1540454741   1e80caffd59e   5 weeks ago   105MB
forklift-4     1540454741   0e3d9c513144   5 weeks ago   105MB
…

"build-docker" 步骤的输出如下:

REPOSITORY                  TAG            IMAGE ID       CREATED          SIZE
forklift-1                  1543511971     13146b26fe19   1 second ago     105MB
forklift-2                  1543511971     7581987997aa   3 seconds ago    105MB
forklift-3                  1543511971     a6ef74a8530e   6 seconds ago    105MB
forklift-4                  1543511970     a7087154d731   10 seconds ago   105MB
<none>                      <none>         cfc622dd7b3c   3 hours ago      105MB
<none>                      <none>         f17e20778baf   3 hours ago      105MB
<none>                      <none>         75cc06f4b5ee   3 hours ago      105MB
<none>                      <none>         1762b4f89680   3 hours ago      105MB
openjdk                     8-jre-alpine   2e01f547f003   5 weeks ago      83MB

但是第二步的输出没有任何标签,尽管图像的大小大致相当:

REPOSITORY   TAG      IMAGE ID       CREATED       SIZE
<none>       <none>   cfc622dd7b3c   3 hours ago   105MB
<none>       <none>   f17e20778baf   3 hours ago   105MB
<none>       <none>   75cc06f4b5ee   3 hours ago   105MB
<none>       <none>   1762b4f89680   3 hours ago   105MB

标签丢失在哪里?

请注意,第二步 (docker image ls) 中的一些图像 ID 似乎与第一步中打印的相同。

P.S。我知道如果我需要标签(例如发布),我可以一步完成构建和发布。

虽然我无法找到根本原因,但我根据 Docker 的 save and load commands and Bitbucket Pipelines' artifacts.

做了一个简单的解决方法

首先,我稍微更改了标记方案:

configure<DockerExtension> {
    configure(this.getProperty("javaApplication"), closureOf<DockerJavaApplication> {
        baseImage = "openjdk:8-jre-alpine"
        tag = "${name}:${System.getenv("DOCKER_TAG")}"
    })
}

因此,我依赖于可以在外部设置的环境变量 DOCKER_TAG,而不是 UTC 时间戳。

然后,定义"build-docker"步骤如下:

- step: &build-docker
    name: Build Docker images
    image:
      name: openjdk:8
    services:
      - docker
    script:
      - export DOCKER_TAG=${BITBUCKET_BUILD_NUMBER}
      - ./gradlew dockerBuildImage
      - docker save
        --output images.tar
        forklift-1:${DOCKER_TAG}
        forklift-2:${DOCKER_TAG}
        forklift-3:${DOCKER_TAG}
        forklift-3:${DOCKER_TAG}
    artifacts:
      - images.tar
    caches:
      - gradle-wrapper
      - gradle

我可以使用内部版本号作为标签,但可以提供任何值。

最后推送图片的步骤是:

- step: &publish-docker
    name: Publish Docker images
    image:
      name: docker
    services:
      - docker
    script:
      - docker load --input images.tar
      - docker image ls
      - docker push …

这行得通,因为 docker save

Produces a tarred repository to the standard output stream. Contains all parent layers, and all tags + versions, or specified repo:tag, for each argument provided.