Production
Build production images
How frappe-nix turns a bench into an immutable build, which OCI images come out of it, and how to build, load, push and run them.
On this page
This page explains how frappe-nix turns a Frappe bench into an immutable build, which container images come out of it, and how to build and run them. Use it when you want a Frappe deployment where nothing is installed or compiled at container start.
If you would rather run Frappe directly on a NixOS host, without containers, see Run Frappe as a NixOS service. It consumes the same builtBench package.
How the immutable bench is built
frappe-nix builds one package, builtBench, and every image is assembled from it. The package contains the apps, the production Python environment, Node and the compiled assets. nix build .#default builds the same thing.
Python
The Python environment comes from uv.lock through uv2nix. Commit uv.lock and the build reads it. Nothing runs uv sync at build or start time.
node_modules from each yarn.lock
Every app with a package.json is a node target. So is each immediate subdirectory of an app that has its own package.json, such as a nested frontend. Each target gets its own node_modules, reproduced from its yarn.lock:
frappe-nix reads the lockfile at evaluation time and creates one
fetchurlper tarball, using the URL andintegritythe lock already records, or the#sha1suffix in older locks. Git dependencies are fetched by commit.The tarballs are linked into an offline mirror.
nixpkgs'
yarnConfigHookrunsyarn install --offline --frozen-lockfile --ignore-scripts --ignore-engines --ignore-platformagainst that mirror.
You commit no hashes. The derivation sees only package.json, yarn.lock, .yarnrc and .npmrc, so editing other files in an app does not rebuild its node_modules.
Lifecycle scripts do not run in the sandbox. The frontends frappe-nix was built against need only prebuilt or platform-specific packages, so this works for them. The one exception is a patch-package postinstall whose patches come from a link: dependency: the install applies those patches itself. If an app does need extra build steps, nodeOverrides.<TARGET> lets you add attributes such as postPatch or nativeBuildInputs to that target's derivation.
An app's own yarn.lock always wins. If an app ships a package.json but no yarn.lock, generate a fallback from the bench's dev shell and commit it:
bench-update --node-locksThat writes node-locks/<APP>/yarn.lock in a bench, or nix/node-locks/ in app mode through nix run .#relock. Evaluation warns when a fallback is older than its package.json, and when one is unused because the app now ships its own lock. It also warns about a target with no lock at all. The package then has no node_modules for it, and builtBench fails on any such app that has a build script.
A lock that does not resolve offline, and the two ways around it, are covered in Dependencies and locks.
Compiling assets
builtBench copies the unbuilt bench tree into the build directory and runs bench build --production --force inside the Nix sandbox. The build sets ESBUILD_TARGET from the esbuildTarget option, which defaults to es2022, and NODE_OPTIONS=--max-old-space-size=4096. --force builds frappe's own assets too, instead of first trying to download frappe's prebuilt ones: the sandbox has no network, and the download would find its release tag with git in apps/frappe, which is not a repository there. The finished tree, including nested frontend output and rewritten asset links, becomes the package.
In a bench, the build also copies the bench root's config/ directory into the package when it exists. That is how an nginx configuration reaches the nginx image, see below. Asset builds explains the esbuild preload that applies here too.
Because the environment, node_modules and assets are all fixed at build time, a given set of locks and app commits produces the same bench every time.
The OCI images
Enable the images in your bench flake:
frappe-nix = {
enable = true;
name = "<BENCH_NAME>";
containers.enable = true;
};Each image is built with dockerTools.buildLayeredImage, tagged latest, and named <name>/<role>, or <registry>/<name>/<role> with containers.registry set (see Building, loading and pushing). The package that builds it is container-<role>. The unified runtime is on by default, which gives you three images.
| Package | Image | Contents |
|---|---|---|
container-runtime | <BENCH_NAME>/runtime:latest | One frappe-runtime process: web, realtime, background jobs and scheduler. Listens on 8000. |
container-nginx | <BENCH_NAME>/nginx:latest | nginx plus the built bench. Listens on 80. |
container-bench | <BENCH_NAME>/bench:latest | The bench command for migrations and one-off tasks. |
Every image carries OCI labels, the ones that serve HTTP have a healthcheck, and they run as a non-root user.
The split image set
Set runtime.enable = false and frappe-nix builds the classic split instead. You get eight packages instead of three:
| Package | Image | Process |
|---|---|---|
container-web | <BENCH_NAME>/web:latest | gunicorn on port 8000 |
container-scheduler | <BENCH_NAME>/scheduler:latest | bench schedule |
container-worker-default | <BENCH_NAME>/worker-default:latest | bench worker --queue default |
container-worker-short | <BENCH_NAME>/worker-short:latest | bench worker --queue short |
container-worker-long | <BENCH_NAME>/worker-long:latest | bench worker --queue long |
container-socketio | <BENCH_NAME>/socketio:latest | Node realtime server on port 9000 |
container-nginx | <BENCH_NAME>/nginx:latest | nginx on port 80 |
container-bench | <BENCH_NAME>/bench:latest | bench |
With the default runtime.enable = true, the container-web package does not exist. Build container-runtime instead.
The old package names
Before 1.2.0 the packages were named by role alone (runtime, nginx, web, …), and container-bench was bench-cli. The image names have not changed. Through 1.x the old package names built the same images with a warning, and containers.aliases = false dropped them. Both went in 2.0.0: build container-<role>, and remove containers.aliases from the flake, which is refused now. See 1.x to 2.0.
Building, loading and pushing
Build an image from your bench repository:
nix build .#container-runtime
docker load < resultresult is an image tarball, and docker load registers it as <BENCH_NAME>/runtime:latest. Repeat with .#container-nginx and .#container-bench for the other two.
Set containers.registry and the images are named for the registry, so they load ready to push. The registry goes in front of the name with a slash, and a trailing slash on it is dropped. With it empty, the default, the names stay bare.
frappe-nix.containers.registry = "registry.example.com";nix build .#container-runtime
docker load < result # registry.example.com/<BENCH_NAME>/runtime:latest
docker push registry.example.com/<BENCH_NAME>/runtime:latestfrappe-nix does not push anything itself. Without a registry set, docker tag the bare name first:
docker tag <BENCH_NAME>/runtime:latest registry.example.com/<BENCH_NAME>/runtime:latestLabels
Every image carries the org.opencontainers.image.* labels:
| Label | Value |
|---|---|
title | the image name, such as <BENCH_NAME>/runtime |
source | https://github.com/Avunu/frappe-nix |
version | the frappe-nix version that built the image |
revision | frappe-nix's commit, when your flake has frappe-nix as the frappe-nix input, as the scaffold does |
licenses | MIT |
They describe frappe-nix. To describe your bench instead, set containers.labels, which goes over them and can add any other label:
frappe-nix.containers.labels = {
"org.opencontainers.image.source" = "https://github.com/acme/bench";
"org.opencontainers.image.revision" = self.rev or "dirty";
};Running the images
Every image except nginx and socketio starts through the same entrypoint. It builds the site's configuration from environment variables and mounted secrets, then runs the image's command.
Running as non-root
Every image runs as frappe, uid 1000 and gid 1000 (containers.nonRoot, true by default since 2.0.0). Through 1.x the images ran as root unless you set it. To keep running as root for now:
frappe-nix.containers.nonRoot = false;With it on, the image owns what that user writes: /bench/sites, /bench/logs, /bench/config (where the scheduler takes its lock) and the home directory /home/frappe, and in the nginx image also /var/log/nginx, /var/cache/nginx and /tmp/nginx. /tmp is writable by everyone.
A new named volume mounted at
/bench/sitestakes the image's ownership, so it needs nothing.A bind mount, or a volume from a host path, must be writable by uid 1000:
chown -R 1000:1000 <DIR>on the host.Secret files in
/secretsmust be readable by uid 1000.
A sites volume that a root image wrote (a 1.x image, or one built with nonRoot = false) is owned by root, so a non-root image cannot write to it, and the entrypoint fails on site_config.json. Before the first start of a non-root image on an existing deployment, change the volume's owner once, with the image's own chown:
docker run --rm --user 0 --entrypoint chown \
-v <BENCH_NAME>-sites:/bench/sites \
<BENCH_NAME>/runtime:latest -R 1000:1000 /bench/sitesnginx listens on port 80, below 1024. Docker lets any user bind those ports in a container's network namespace, but rootless Podman and some Kubernetes runtimes do not by default. With nonRoot on, run the nginx container there with the sysctl that allows it: --sysctl net.ipv4.ip_unprivileged_port_start=0 in Podman, or a pod securityContext.sysctls entry in Kubernetes. Or have your nginx.conf listen on a port above 1024 and publish that one.
Healthchecks
The images that serve HTTP have a HEALTHCHECK. Docker and Podman run it every 30 seconds, after a 120-second start period, and mark the container unhealthy after three failures in a row:
| Image | Healthy when |
|---|---|
runtime, web | GET /api/method/ping on port 8000, for the site in FRAPPE_SITE, answers 200 |
socketio | the realtime server on port 9000 completes a Socket.IO handshake |
nginx | nginx on port 80 answers with any HTTP status. The runtime behind it has its own healthcheck. |
scheduler, the workers and bench serve nothing and have none. The probes use the image's own Python, Node or Bash, so the images need no curl. A runtime started on a unix socket instead of port 8000 has nothing for its probe to reach; run it with --no-healthcheck, or give it a --health-cmd of your own.
Volumes
Mount two paths:
/bench/sitesis a persistent volume for site state (writable by uid 1000, unlesscontainers.nonRoot = false)./secretsholds secret files, mounted read-only.
At every start the entrypoint links apps.txt, apps.json and the compiled assets from the image into /bench/sites, so a new image always brings its own app registry and assets. common_site_config.json is yours, apart from the three Redis keys described under Environment variables.
Environment variables
FRAPPE_SITE is required. Without it the container exits immediately. The rest have defaults you should usually override:
| Variable | Default | Becomes |
|---|---|---|
FRAPPE_DB_HOST | 127.0.0.1 | db_host |
FRAPPE_DB_PORT | 3306 | db_port |
FRAPPE_DB_TYPE | mariadb | db_type |
FRAPPE_DB_NAME | the site name, with dots replaced by underscores | db_name |
FRAPPE_DB_USER | the same default as FRAPPE_DB_NAME | db_user |
FRAPPE_REDIS_CACHE | redis://127.0.0.1:13000 | redis_cache, also bench-wide (see below) |
FRAPPE_REDIS_QUEUE | redis://127.0.0.1:13000 | redis_queue, also bench-wide |
FRAPPE_REDIS_SOCKETIO | redis://127.0.0.1:13000 | redis_socketio, also bench-wide |
FRAPPE_DB_SOCKET | unset | db_socket, when set |
FRAPPE_SOCKETIO_UDS | unset | socketio_uds, when set |
The entrypoint writes the variables to the site's site_config.json, and also merges the three Redis URLs into sites/common_site_config.json. Both are needed:
The split set's workers serve every site, so they read
redis_queuefromcommon_site_config.json, not from a site's config.The
socketioimage takes noFRAPPE_REDIS_*variables of its own and reads them from that file too.Without the bench-wide keys, both fall back to Frappe's default ports, such as 11311 for the queue.
In common_site_config.json:
A variable that is set wins.
When a variable is unset, a key you set yourself is kept.
A key that is missing gets the default above.
The entrypoint rewrites the file only when it changes, through a temporary file and a rename, so containers that share the sites volume never read a partial file. Give every container that shares the volume the same Redis variables.
The images also set FRAPPE_BENCH_ROOT=/bench, SITES_PATH=/bench/sites, FRAPPE_ENV_TYPE=production, FRAPPE_STREAM_LOGGING=1 and FRAPPE_TUNE_GC=1, and GIT_PYTHON_REFRESH=quiet, without which bench fails at startup on an image that has no git.
Secrets
The entrypoint merges these files into site_config.json, in this order:
/secrets/db_passwordbecomesdb_password./secrets/encryption_keybecomesencryption_key.Every
/secrets/*.jsonis deep-merged over what is already there, which is the place for object-storage credentials and similar settings.
FRAPPE_DB_SOCKET and FRAPPE_SOCKETIO_UDS are merged after that, when set. The entrypoint writes the result with mode 0600. Secrets never enter the image or the Nix store.
A single-host example
This starts the runtime image against a MariaDB and Redis you already have:
docker run -d --name <BENCH_NAME>-runtime \
-p 8000:8000 \
-e FRAPPE_SITE=site1.example.com \
-e FRAPPE_DB_HOST=<DB_HOST> \
-e FRAPPE_REDIS_CACHE=redis://<REDIS_HOST>:6379 \
-e FRAPPE_REDIS_QUEUE=redis://<REDIS_HOST>:6379 \
-e FRAPPE_REDIS_SOCKETIO=redis://<REDIS_HOST>:6379 \
-v <BENCH_NAME>-sites:/bench/sites \
-v <SECRETS_DIR>:/secrets:ro \
<BENCH_NAME>/runtime:latestThe default command is frappe-runtime --host 0.0.0.0 --port 8000 --job-threads 4, with /bench as the working directory. The runtime is not started with --dev, the flag that makes it serve /assets and /files itself, so put nginx in front of it for /assets.
One-off commands
Run bench commands from the bench image with the same variables and volumes:
docker run --rm \
-e FRAPPE_SITE=site1.example.com \
-e FRAPPE_DB_HOST=<DB_HOST> \
-v <BENCH_NAME>-sites:/bench/sites \
-v <SECRETS_DIR>:/secrets:ro \
<BENCH_NAME>/bench:latest bench --site site1.example.com migratefrappe-bench's bench refuses to run as root, so this needs containers.nonRoot, the default. With nonRoot = false, call Frappe's own command line, which bench hands these commands to anyway: python -m frappe.utils.bench_helper frappe --site site1.example.com migrate. This is only about commands you run yourself: the split set's scheduler and worker-* images start that command line directly, so they run as root or not.
The nginx image
The nginx image runs nginx -c /bench/config/nginx.conf -g "daemon off;". frappe-nix does not generate that file. The build copies your bench's config/ directory into the image, so you supply config/nginx.conf yourself.
The .gitignore that frappe-nix scaffolds excludes config/*.conf. A flake only sees tracked files, so add your file with git add -f config/nginx.conf, or it never reaches the image.
What the images do not do
The entrypoint writes
site_config.jsononly. It does not create the site's database, so create or restore the site yourself.frappe-nix ships no compose file or orchestration for these images.
They do not carry a database or Redis. The process images include common runtime dependencies, such as
wkhtmltopdf,chromium, fonts and the MariaDB client, andextraContainerRuntimeDepsadds more.