一般來說,如果是透過GCP官網的介紹方式,透過 docker 指令推送 image 到 GCP Contanier Registry 上,
如果未指定版號推送
$ docker push asia.gcr.io/xxxxxx/darwin-image-test
The push refers to repository [asia.gcr.io/xxxxxx/darwin-image-test]
a65ff0f5005b: Pushed
2acf82036f38: Layer already exists
9f65d1d4c869: Layer already exists
0f804d36244d: Layer already exists
9b23c8e1e6f9: Layer already exists
ffd3d6313c9b: Layer already exists
9eb82f04c782: Layer already exists
latest: digest: sha256:xxxxxx size: 1777
2acf82036f38: Layer already exists
9f65d1d4c869: Layer already exists
0f804d36244d: Layer already exists
9b23c8e1e6f9: Layer already exists
ffd3d6313c9b: Layer already exists
9eb82f04c782: Layer already exists
v01: digest: sha256:xxxxxx: 1570
推送完 image 後,檢查目前在 GCP Contanier Registry 上的 image 會自帶 latest 的 tag
$ gcloud container images list-tags asia.gcr.io/xxxxxx/darwin-image-test
DIGEST TAGS TIMESTAMP
ce95dbb8bbb6 latest 2021-02-28T22:27:23
b08ecc9f7997 v01 2021-02-18T03:20:26
不過近日在接觸某些專案時,看到以下的 image 清單:
$ gcloud container images list-tags asia.gcr.io/xxxxxx/darwin-image-test
DIGEST TAGS TIMESTAMP
04465a0bc048 2021-02-28T22:33:14
ce95dbb8bbb6 latest 2021-02-28T22:27:23
b08ecc9f7997 v01 2021-02-18T03:20:26
04465a0bc048 這個image id 的 tag 是『空白的』、『空白的』、『空白的』
花惹發!正常 docker 推送不是會自帶 latest 版號嗎 ?
後來探究原因,造成這個現象發生的兇手就是直接在 gcloud 的路徑下直接執行 gcloud 裡面的 docker 推送所造成的
$ /usr/local/bin/gcloud docker -- push asia.gcr.io/xxxxxx/darwin-image-test
WARNING: `gcloud docker` will not be supported for Docker client versions above 18.03.
As an alternative, use `gcloud auth configure-docker` to configure `docker` to
use `gcloud` as a credential helper, then use `docker` as you would for non-GCR
registries, e.g. `docker pull gcr.io/project-id/my-image`. Add
`--verbosity=error` to silence this warning: `gcloud docker
--verbosity=error -- pull gcr.io/project-id/my-image`.
See: https://cloud.google.com/container-registry/docs/support/deprecation-notices#gcloud-docker
The push refers to repository [asia.gcr.io/xxxxxx/darwin-image-test]
d8e9a7a6fa33: Pushed
2acf82036f38: Layer already exists
9f65d1d4c869: Layer already exists
0f804d36244d: Layer already exists
9b23c8e1e6f9: Layer already exists
ffd3d6313c9b: Layer already exists
9eb82f04c782: Layer already exists
latest: digest: sha256:xxxxxx size: 1777
如何在 CGP Container Registry 上撈出這些沒 tag 的 images 呢?
參考官網教學:
$ gcloud container images list-tags asia.gcr.io/xxxxxx/darwin-image-test --filter="NOT tags:*"
DIGEST TAGS TIMESTAMP
04465a0bc048 2021-02-28T22:33:14
在現今強調 CI/CD 的潮流下,不管是 docker registry、GitLab、資料庫 ...
只要養成良好的習慣,其實可以避開相當多的問題。
以本文 docker image 為例:『在推送image前,永遠tag接上git版號 ,那麼一來再清docker registry時才不會猶豫不決。另外,即便是測試檔也給一個特定的tag例如:test字樣,讓推上去的image永遠迭代掉同一個檔案,才不會三不五時就遇到空間爆掉的問題,甚至是看到恐怖的 GCP 使用帳單!