Writing a service · all pages

Start

Guides

Reference

Project

  1. Docs
  2. Guides
  3. Writing a service

Writing a service

Turn any service, a container image or a Rust binary into a release, and the contract it carries.

  • 5 min read
  • 1 sections

There are four ways in. All of them end in diaporos push.

A Node.js checkout: build it as usual (npm ci && npm run build), then let init write the tree and a contract from what the repo already says:

diaporos init ../my-api out/
# name       my-api                             package.json name
# port       http 3005                          .env PORT
# health     http /health-check                 route in src/app.js
# exec       bin/node dist/server               Dockerfile.pipeline CMD → npm run startup
# env        PORT ${DIAPOROS_PORT_HTTP}         the node assigns the port
# memory     1024Mi                             default
# grants     39 env.read                        .env.example, .env
# tree       out/ (26802 files, 3668 dirs, 66 links, 244.20 MiB) from dist, node_modules, ...
# note       health path `/health-check` is a route in ...; fix it if the router is mounted under a prefix
# contract   out/diaporos.contract
diaporos secret import my-api ../my-api/.env --contract out/   # values, on the node
diaporos push out/
Contract line Inferred from (first match)
exec --entry; a Dockerfile* ENTRYPOINT/CMD (test/dev/local/debug files skipped), followed through sh -c, tini, VAR=x, npm run/yarn/pnpm scripts to node [flags] FILE [args]; the start:prod, startup, serve, start scripts; main; dist/main.js and friends
port --port; PORT in .env*; Dockerfile EXPOSE; 3000 (noted)
health --health; a Kubernetes/Helm httpGet probe; Dockerfile HEALTHCHECK; compose healthcheck; a health route in the sources (noted); TCP (noted)
memory --memory; a Kubernetes limits.memory; 1024Mi
env VAR=x from the start command and plain Dockerfile ENV; PORT is always the node’s
cap env.read every name in .env.example/.sample/.template/.defaults/.production/.env, plus NODE_ENV, HOME. Names only: values are never copied

It ships package*.json, tsconfig*.json, node_modules, the entry’s directory, COPY sources from the Dockerfile and --include paths, never .env* or .npmrc, plus the node on PATH (or --node-binary) as bin/node, then runs bundle (--no-bundle skips it). Files are cloned on APFS and hard-linked elsewhere when possible. Every line carries its source as a comment, and guesses come with note lines (node version vs .nvmrc, dev dependencies, macOS builds). Edit the contract freely: running init again after a build refreshes the files and keeps it. It refuses an <out> that holds anything but a previous init.

Any language: put the runtime and the app in one directory with a diaporos.contract at its root and an exec line, e.g. exec bin/node dist/main. The node runs it from the release directory (or workdir). Then run diaporos bundle <dir>: it copies the shared libraries the tree’s executables and native addons load into <dir>/lib and puts that directory on the contract’s library path, so the tree no longer depends on the build host’s /usr/lib. It reads ELF and Mach-O headers and never runs anything. The host still provides the C library and its loader; the report says which (host needs ld-linux-aarch64.so.1, glibc ≥ 2.36), so build on the oldest system you deploy to. Ready-made scripts:

Example Tree
examples/node-tree/make-tree.sh <checkout> <out> bin/node, dist/, node_modules/, lib/ what init does by hand, with a fixed contract
examples/python-tree/make-tree.sh <app-dir> <out> bin/python3, the stdlib, requirements.txt installed, app/, lib/ precompiled byte code, so the service never writes into its release
examples/go-svc/make-tree.sh <out> bin/go-svc (static) the contract is a file kept beside main.go

From a container image: if the service already has a Dockerfile, import the image:

diaporos import my-api:1.4 out/            # or docker save output, or an OCI layout
# image      my-api:1.4 (linux/arm64/v8, 6 layers)
# tree       out/ (2585 files, 650 dirs, 343 links, 156.12 MiB)
# exec       lib/ld-musl-aarch64.so.1 --library-path ... ${DIAPOROS_RELEASE}/usr/local/bin/node server.js
# workdir    app
# port       http 3000
# note       dropped the image's entrypoint `docker-entrypoint.sh`: ...; pass --exec to keep it
# note       declared port 3000 as `http` with a TCP health check; under the node daemon the app must listen on $PORT ...
# contract   out/diaporos.contract
diaporos push out/

The layers are applied in order (whiteouts included) and the image config becomes the contract: Entrypoint + Cmd → exec, WorkingDir → workdir, Env → env, ExposedPorts → ports with cap net.listen and a TCP health check, and PORT → the node-assigned port. A dynamically linked program runs through the image’s own loader with the image’s library directories, so a musl image runs on a glibc host and the other way round, with nothing installed on the host. --exec "PROGRAM ARGS" replaces the image’s command. The note lines list what didn’t carry over: User, Volumes, Healthcheck, UDP ports, shell-form commands that need a shell. See Known limitations.

Rust, single binary (examples/hello-svc):

  1. Depend on diaporos-sdk (no transitive deps).
  2. Declare the contract with diaporos_sdk::contract!{ ... } at module level.
  3. Call diaporos_sdk::handle_flag(DIAPOROS_CONTRACT) first thing in main. This makes --diaporos-contract print the contract, which helps with debugging.
  4. Bind with diaporos_sdk::listen_addr("http", "127.0.0.1:8080"). It uses the node-assigned address under diaporos and the default under cargo run.
  5. Read only the environment variables you granted. Everything else is scrubbed.

diaporos run deploys a single binary; the node daemon deploys trees, so put the binary in a directory with a diaporos.contract for zero-downtime deploys.

Contract directives (v1)

Directive Example Meaning
name name hello-svc required; [A-Za-z0-9_-]
version version 0.1.0 informational
port port http 8080 named port; must also be granted with cap net.listen. Under diaporos node the release gets a private port: read it via DIAPOROS_LISTEN_<NAME> or put ${DIAPOROS_PORT_<NAME>} in env/exec (e.g. env PORT ${DIAPOROS_PORT_HTTP}). A fixed number there (env PORT 9000) never answers the health check; push warns
exec exec bin/node dist/main tree artifacts: program (inside the tree) and args. Args may use ${DIAPOROS_RELEASE} and ${DIAPOROS_PORT_<NAME>}
workdir workdir app tree artifacts: working directory, relative to the release (default its root)
env env LD_LIBRARY_PATH ${DIAPOROS_RELEASE}/lib fixed variable; ${DIAPOROS_RELEASE} = release dir. Not DIAPOROS_*, not also env.read
health health http /health HTTP GET on a declared port; 2xx means healthy. health tcp <port> = accepts a connection
cpu cpu 250m millicores; sandbox: cgroup cpu.max
memory memory 64Mi MiB; sandbox: cgroup memory.max (an OOM kill is an oom event, then a restart)
cap net.listen cap net.listen http may listen on that named port; sandbox: Landlock TCP bind (kernel 6.7+)
cap net.connect cap net.connect postgres:5432 may connect to that target; sandbox: Landlock TCP connect, by port only (kernel 6.7+)
cap env.read cap env.read GREETING the node supplies this variable from its secrets (enforced by every runner; see diaporos secret)
cap fs.read / fs.write cap fs.read /etc/ssl/certs absolute path; sandbox: Landlock
depends depends postgres parsed, not acted on yet (ordering and discovery hint)

# starts a comment. “Sandbox” means --runner sandbox on Linux; the default process runner enforces only env.read. --strict refuses to start a service whose grants this host can’t enforce, and the dashboard shows grants against enforcement.

↑↓ to move ↵ to open