如何阻止“gcloud 组件更新”保留备份?

Fel*_*lix 5 docker google-cloud-platform gcloud

我正在构建一个 google-cloud-sdk 基础映像,并试图保持它的精简。

ARG GCLOUD_SDK_VERSION=285.0.1-alpine
FROM google/cloud-sdk:$GCLOUD_SDK_VERSION

# Install Java 8 for Datastore emulator
RUN apk add --update --no-cache \
        openjdk8-jre
RUN gcloud components install \
        cloud-datastore-emulator \
        pubsub-emulator \
        beta \
        --quiet
...
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好。当我构建并查看gcloud components install命令的输出时,我充满信心:

????????????????????????????????????????????????????
?       These components will be installed.        ?
????????????????????????????????????????????????????
?           Name           ?  Version   ?   Size   ?
????????????????????????????????????????????????????
? Cloud Datastore Emulator ?      2.1.0 ? 18.4 MiB ?
? Cloud Pub/Sub Emulator   ? 2019.09.27 ? 34.9 MiB ?
? gcloud Beta Commands     ? 2019.05.17 ?  < 1 MiB ?
????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

然而,我的最终图像大小令人震惊:1.11GB。当我查看 docker 历史时,我发现组件安装实际上为最终图像大小贡献了 654MB:

CREATED BY                                      SIZE
/bin/sh -c gcloud components install        …   654MB
/bin/sh -c apk add --update --no-cache      …   78.2MB
Run Code Online (Sandbox Code Playgroud)

I am currently assuming it has something to do with the final line of the installation process.

??????????????????????????????????????????????????????????????
?? Creating backup and activating new installation          ??
??????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

That appears to be a nice thing to have on your local workstation but not in a docker image. I tried to see if I can deactivate this backup or delete it afterward. There was no helpful paragraph in the man pages and searching online always directs me to some cloud database backup procedures you can initiate using gcloud commands.

Does anyone have further knowledge about what is happening here? Maybe it is not the backup at all that is contributing to most of the overhead?

Fel*_*lix 6

首先我要感谢@gso_gabriel 的建议。我最终分析了调用之前和之后的文件系统利用率gcloud components install

  1. 摆脱备份原来很容易。在 gcloud 应用程序文件夹中有一个隐藏的隐藏.install/.backup文件夹,它在开始时并不存在,在组件安装后占用了大约 270 MB。
  2. 安装组件还留下了一堆__pycache__文件夹,总计大约 100 MB。

为了减小整体图像大小,我将相应的 docker 行更新为:

RUN gcloud components install \
        cloud-datastore-emulator \
        pubsub-emulator \
        beta \
        --quiet \
    && rm -rf $(find google-cloud-sdk/ -regex ".*/__pycache__") \
    && rm -rf google-cloud-sdk/.install/.backup
Run Code Online (Sandbox Code Playgroud)

可以通过以下方式看到改进docker history

CREATED BY                                      SIZE
/bin/sh -c gcloud components install        …   317MB
Run Code Online (Sandbox Code Playgroud)

减少了 300 MB 以上。我通过使用它作为我的一个应用程序的数据存储和发布订阅服务来测试图像,运行它的单元测试。一切似乎仍然运作良好。

可能还有更多文件可以清理,但我相信我已经得到了重量级文件。