Skip to content

Commit

Permalink
docs: warn about layer stacking strategies
Browse files Browse the repository at this point in the history
Layers won't stack if they're siblings. They'll only stack if they're parents.

It is better to be clear about this subject to avoid surprises.

@moduon MT-1075
  • Loading branch information
yajo committed Apr 11, 2024
1 parent 2154ad0 commit eaff1a6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,33 @@ 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!!!"
'';
in
pkgs.nix2container.buildImage {
name = "hello";
config = {
entrypoint = ["${pkgs.bash}/bin/bash" application];
};
layers = [
# These layers might duplicate a store path
(pkgs.nix2container.buildLayer { deps = [pkgs.bash]; })
(pkgs.nix2container.buildLayer { 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

0 comments on commit eaff1a6

Please sign in to comment.