通过Docker构建时如何缓存nix派生的依赖项?

问题描述 投票:1回答:1
FROM nixos/nix@sha256:af330838e838cedea2355e7ca267280fc9dd68615888f4e20972ec51beb101d8
# FROM nixos/nix:2.3
ADD . /build
WORKDIR /build
RUN nix-build
ENTRYPOINT /build/result/bin/app

我上面有一个非常简单的Dockerfile,可以成功构建我的应用程序。但是,每次我修改应用程序目录(.)中的任何文件时,都必须从头开始重建并下载所有nix存储依赖项。

我可以以某种方式获取下载的存储依赖项的“列表”,然后将其添加到Dockerfile的开头,以独立地对其进行缓存(以节省时间和带宽为最终目标)?

[我知道我可以使用本身具有缓存功能的nix(以及nix存储)来构建此docker映像,但是我试图在非nix环境中(因此使用docker)使其具有可构建性。

nix
1个回答
0
投票

我可以建议将源分为两部分。这个想法是创建一个仅包含依赖项的单独的Docker层,该层很少更改:

FROM nixos/nix:2.3
ADD ./default.nix /build
# if you have any other Nix files, put them to ./nix subdirectory
ADD ./nix /build/nix
# now let's download all the dependencies
RUN nix-shell --run exit

# At this point, Docker has cached all the dependencies. We can perform the build
ADD . /build
WORKDIR /build
RUN nix-build
ENTRYPOINT /build/result/bin/app
© www.soinside.com 2019 - 2024. All rights reserved.