如何使用 `gcloud config` 构建基于 `gcr.io/google.com/cloudsdktool/cloud-sdk` 的镜像

How to use `gcloud config` to build an image based on `gcr.io/google.com/cloudsdktool/cloud-sdk`

我正在尝试基于 gcr.io/google.com/cloudsdktool/cloud-sdk:alpine 构建映像,其中包含一些我在构建时已经知道的配置。在我的本地系统上使用 gcloud config set 进行一些测试后,我将这些命令转置到我的 Dockerfile 中。虽然命令序列在我的计算机上产生了预期的 final gcloud 配置,但我在 docker 图像上发现的行为是不同的,并且在 RUN 命令。

我没想到会有这种行为。这应该怎么解释?

我在故障排除期间使用了以下 Dockerfile 来评估问题:

FROM gcr.io/google.com/cloudsdktool/cloud-sdk:alpine AS runner
  RUN ["gcloud", "config", "list"]
  RUN ["gcloud", "config", "set", "compute/region", "europe-west1"]
  RUN ["gcloud", "config", "list"]
  ENTRYPOINT [ "bash" ]

可以看到步骤 2/5 和步骤 4/5 产生相同的输出,即使步骤 3/4 应该更改 current gcloud 配置 compute/region.

$ docker build --no-cache .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM gcr.io/google.com/cloudsdktool/cloud-sdk:alpine AS runner
 ---> e3ce7ea1a190
Step 2/5 : RUN ["gcloud", "config", "list"]
 ---> Running in b8fb806b04aa
[component_manager]
disable_update_check = true
[core]
disable_usage_reporting = true
[metrics]
environment = github_docker_image

Your active configuration is: [default]
Removing intermediate container b8fb806b04aa
 ---> bc8321e71e60
Step 3/5 : RUN ["gcloud", "config", "set", "compute/region", "europe-west1"]
 ---> Running in cfbcb80b1a31
Updated property [compute/region].
Removing intermediate container cfbcb80b1a31
 ---> db76ee7c5e60
Step 4/5 : RUN ["gcloud", "config", "list"]
 ---> Running in d90c64fcf0b0
[component_manager]
disable_update_check = true
[core]
disable_usage_reporting = true
[metrics]
environment = github_docker_image

Your active configuration is: [default]
Removing intermediate container d90c64fcf0b0
 ---> a593aa61055e
Step 5/5 : ENTRYPOINT [ "bash" ]
 ---> Running in acb4d360754c
Removing intermediate container acb4d360754c
 ---> edab55257026
Successfully built edab55257026

云建设者 https://github.com/GoogleCloudPlatform/cloud-builders/tree/master/gcloud

gcloud 配置保留在 /root/.config/gcloud/configurations/config_default

但是 /root/.config/root/.kubeVOLUME,无法更改:

VOLUME ["/root/.config", "/root/.kube"]

https://github.com/GoogleCloudPlatform/cloud-sdk-docker/blob/8764ec3c23860b35e9a7883c74ca2b4a6b45d866/Dockerfile#L42

"If any build steps change the data within the volume after it has been declared, those changes will be discarded."

参见:https://docs.docker.com/engine/reference/builder/#volume