我可以将RUN命令的所有修改文件复制到新的镜像层中吗?

问题描述 投票:0回答:1

我有一个 Dockerfile,其中一些 Linux 软件包是从源代码编译的。 源代码大小为数千兆字节,而构建工件仅为兆字节。

我编译的包会在文件系统中任何可能的目录中构建和移动文件,例如

/etc
/lib
。由于我不是该包的维护者,我不想手动维护包含构建工件的目录列表。

我正在寻找的是一种方法来保留那些由构建过程创建的修改或创建的文件,这本质上是Dockerfile中的

RUN make
步骤。

下面是一个思想实验,它展示了我想要存档的总体思路。

是否有我应该采用的 Dockerfile 机制或使用模式来归档我上面描述的内容?

FROM ubuntu as build-layer

# 1. Clone source code - This increases the image size by Gigabytes
RUN git clone ...

# 2. Compile the package - This may create files in multiple, unknown directories (like /usr or /etc)
RUN --id=build-step make

FROM ubuntu as runtime-layer

# 3. Copy build artifacts
COPY --from=build-layer  --from-id=build-step

# The runtime-layer should now only be as large as the
# ubuntu image + the build artifacts generated by make install
docker dockerfile
1个回答
0
投票

我相信该功能已被请求过几次,但还不是 buildkit 的功能。最好的办法是在 moby/buildkit 或 docker/roadmap 中打开或评论问题。例如。 moby/buildkit #4239 如果实施的话可能会解决这个问题。

要在您自己的构建中执行此操作,您可以使用 wagoodman/dive 等工具检查图层的更改,然后确保所有这些文件都作为构建的一部分进行复制。

在更高级的层面上,如果您想构建自己的图像清单,这将相当简单,您将创建一个没有

git clone
层的清单。我自己的工具 regclient 甚至提供了在从构建器输出图像后修改图像的功能,因此您可以运行:

regctl image mod --layer-rm-created-by 'git clone' $orig_image --create $new_iamge
© www.soinside.com 2019 - 2024. All rights reserved.