Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: warn about layer stacking strategies #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,60 @@ and a second layer containing the script only.
In real life, the isolated layer can contains a Python environment or
Node modules.

Keep in mind that each layer will only be able to strip out nix store paths
that exist in their _child_ layers (because layers can contain layers), not in
their _sibling_ layers. So, if you did it like this, chances are that there
were some repeated nix store paths between layers 1 and 2. In that case, you
must choose between optimizing image cache or image size:

```nix
{ pkgs }:
let
application = pkgs.writeScript "conversation" ''
${pkgs.hello}/bin/hello
echo "Haaa aa... I'm dying!!!"
'';
layer1 = pkgs.nix2container.buildLayer { deps = [pkgs.bash]; };
layer2 = pkgs.nix2container.buildLayer { deps = [pkgs.hello]; };
in
pkgs.nix2container.buildImage {
name = "hello";
config = {
entrypoint = ["${pkgs.bash}/bin/bash" application];
};
# These layers might duplicate a store path
layers = [layer1 layer2];
}
```

If you have many layers and you want to control their splitting manually, but
still avoid duplicating data, you can just fold them:

```nix
{ lib, pkgs }:
let
application = pkgs.writeScript "conversation" ''
${pkgs.hello}/bin/hello
echo "Haaa aa... I'm dying!!!"
'';
in
pkgs.nix2container.buildImage {
name = "hello";
config = {
entrypoint = ["${pkgs.bash}/bin/bash" application];
};
# Folding layers to avoid duplicating store paths
layers = lib.foldl
(acc: current:
[(pkgs.nix2container.buildLayer (current // {layers = acc;}))])
[]
[
{deps = [pkgs.bash]}
{deps = [pkgs.hello]}
];
}
```

See
[Nix & Docker: Layer explicitly without duplicate packages!](https://blog.eigenvalue.net/2023-nix2container-everything-once/)
for learning how to avoid duplicate store paths in your explicitly layered
Expand Down