Hello, I’d like to create minimal a docker image for a Python project. I created a new project using poetry2nix template (nix flake init --template github:nix-community/poetry2nix
) and added a new target for building docker image. I also added a Flask dependency. However, the resulting image is rather big, it has 163 MB. It contains whole closure of the app. But it seems like there are many packages that are needed only during the build-time but not during the run-time. After running nix-store --query --tree ./result/
, I found out that python depends on bash, which depends on glibc which depends on xgcc. I’d expect there to be only a static binary of python, or python binary with some shared objects.
Is there a way to include only packages that are necessary during the runtime? I guess, I’d like something similar to multistage docker images, where in the first stage you have all build dependencies and the second stage you start from SCRATCH
and include only what is needed during the runtime.
Here is the example project: Matúš Ferech / nix-python-big-docker · GitLab
Here is how I tried to build the docker image:
{
description = "Application packaged using poetry2nix";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
flake-utils,
poetry2nix,
}:
flake-utils.lib.eachDefaultSystem (system: let
# see https://github.com/nix-community/poetry2nix/tree/master#api for more functions and examples.
pkgs = nixpkgs.legacyPackages.${system};
inherit (poetry2nix.lib.mkPoetry2Nix {inherit pkgs;}) mkPoetryApplication;
in {
packages = rec {
app = mkPoetryApplication {
projectDir = self;
};
default = self.packages.${system}.app;
docker = pkgs.dockerTools.buildImage {
name = "app";
tag = "0.1.0";
config = {
Cmd = ["${app}/bin/app"];
};
};
};
devShells.default = pkgs.mkShell {
inputsFrom = [self.packages.${system}.app];
packages = [pkgs.poetry];
};
});
}