如何添加根证书并仅保留 docker 构建时间?

how do I add root certificate and keep only in docker build time?

问题有两部分,第一部分:how to add root certificate?很简单,可以参考

第二部分,也就是我真正想问的是:how to keep the root certificate only in docker build time?

也许我们可以使用 buildctlRUN --mount=type=secret;但它不能涵盖所有情况。

假设我想通过具有自签名证书的站点,例如:

RUN curl https://x01.self-signed-site/obj01
RUN npm install --registry https://x02.self-signed-site/npm
RUN pip install -i https://x03.self-signed-site/pypi/simple
RUN mvn install
...

因此,我们需要为每个工具配置证书:

(prepare certificate and prepare .npmrc, .curlrc, ...)
(for, curl, npm, pip, we can use env vars; but we cannot guarantee we can use this way for other tools)

因此,我们需要将自签名证书下载到映像中,并修改一些文件以应用证书配置。如何仅在构建时保留更改(最终图像中没有持久层)?

我们使用 docker savedocker load 解决了这个问题;但目前,docker load 并没有像我们预期的那样工作(另请参阅

无论如何,下面是我们的伪代码解决方案:

docker save -o out.tar <image>
mkdir contents && cd contents
tar xf ../out.tar
open manifest.json, get config <hash>.json as config.json

remove target layers in:
- config.json[history]
- config.json[rootfs][diff_ids]
- manifest.json[0][Layers]

remove layer tarballs (get layer_hashes from maniefst.josn[0][Layers]):
- <layer_hash>/*

fill gap between missing layers:
- <layer_hash_next>/json[parent] = <layer_hash_prev>

tar cf ../new.tar *
docker rmi <image>
docker load -i ../new.tar

参考:https://github.com/stallpool/track-network-traffic/blob/main/bin/docker_image_cleanup.py