kvbox.cloud
Deploy

Several services

A frontend and an API on one box behind one HTTPS address: list them under services in kvbox.json and deploy them with one command.

An app made of several parts, a React frontend and a Node API say, deploys to one box with one address. Each part is a service. You describe them in a kvbox.json at the top of the project:

{
  "services": {
    "web": { "template": "react", "dir": "apps/web", "paths": ["/"] },
    "api": { "template": "node",  "dir": "apps/api", "port": 8000, "paths": ["/api"] }
  }
}
kvbox deploy
# ✓ shop is live at https://shop.kvbox.app
#   web · React · / · 14 files, 1.2 MB
#   api · Node server · /api · port 8000 · 31 files, 80 KB

The first run asks the box name, the address and the box size, and writes them into the file. After that it asks nothing.

How requests reach a service

The address points at nginx in the box. It sends each path to its service:

https://shop.kvbox.app/          → web   (its built files)
https://shop.kvbox.app/api/...   → api   (port 8000 in the box)
  • The path is passed on as it is: /api/users reaches the API as /api/users.
  • A path takes everything under it: /api is also /api/users, never /apix.
  • Frontend and API share one origin, so the frontend calls /api/... with no CORS setup and no API address to configure.
  • WebSockets and streamed answers (server-sent events) pass through.

A service

FieldMeaning
templatereact, static, node, nextjs or docker, the same templates as one app, or postgres (see below)
dirthe service's directory, relative to kvbox.json
pathsthe URL paths it answers. Required for react and static; a server without one is reached by the other services only
portthe port of a node, nextjs or docker service in the box (default: the Dockerfile's EXPOSE, else 3000). Each service needs its own, and 80 is taken
contexta docker service's build context when it is not dir, for a Dockerfile that copies files from the project root. The Dockerfile stays in dir
distthe build output folder of a react or static service, when it is not dist or build
releasea command run on every deploy before the new version goes live, for a node, nextjs or docker service (see below)

A service name is lowercase letters, digits and -. app, agent, init and volumes are taken.

A server must listen on process.env.PORT, which is its port. Services reach each other inside the box on http://127.0.0.1:<port>.

A frontend served under a path other than / must be built for it (Vite: base: "/docs/").

A Docker service

A service with a Dockerfile is built into an image in the box and run as a container:

"api": { "template": "docker", "dir": "apps/api", "context": ".", "paths": ["/api"] }
  • context: "." ships the whole project (without node_modules, .git and .venv) and builds with apps/api/Dockerfile, as docker build -f apps/api/Dockerfile . would from the project root. Without context, only dir is shipped.
  • The container runs on the box's own network, with no port mapping: it must listen on its port, which it also gets as PORT. It reaches the other services on 127.0.0.1 like they reach it.
  • It gets the box's environment variables each time it starts.
  • Its output is its service's log: journalctl -u kvbox-api.

A release command

A service with a server can name one command to run on every deploy, before its new version replaces the running one. It is made for database migrations:

"api": { "template": "docker", "dir": "apps/api", "paths": ["/api"], "release": "alembic upgrade head" }
  • It runs after the build, in the new version: for node and nextjs in the new source, installed and built; for docker in a container of the new image (sh -c, so the image needs a shell; the image's entrypoint is kept).
  • It gets the box's environment variables, the database's URL included, and PORT.
  • If it fails, that service's deploy stops there and shows the command's last lines. Nothing was replaced: the version already running keeps running.
  • The old version is still serving while it runs, so for those seconds it works against the migrated database. Write migrations the old code can live with (add a column first, remove one a deploy later).
  • It runs on every deploy of that service, so it must be safe to run twice. Migration tools are.

A Postgres database

A postgres service runs a database in the box, with its data on a volume:

{
  "services": {
    "db":  { "template": "postgres", "env": "DATABASE_URL" },
    "api": { "template": "node", "dir": "apps/api", "paths": ["/api"] }
  }
}

The first deploy asks for the volume's name and size (default <box>-db, 10 GB) and writes them to kvbox.json:

"volumes": { "shop-db": "10GB" },
"services": { "db": { "template": "postgres", "env": "DATABASE_URL", "volume": "shop-db" }, … }

Then it:

  1. makes a password and puts the database's URL in the box variable env (default DATABASE_URL), as a secret: postgres://kvbox:<password>@127.0.0.1:5432/app. Every service reads it.
  2. makes the volume and attaches it to the box. This restarts the box once.
  3. starts the database before the other services.

After a stop and start of the box, or a wake, every service with a server waits for the database to answer before it starts (up to 60 seconds, then it tries again).

FieldMeaning
imagethe Postgres image, default postgres:17. For vectors: pgvector/pgvector:pg17
envthe box variable the URL goes in, default DATABASE_URL; each database needs its own
volumethe volume holding the data, a name from volumes
portdefault 5432
  • The database listens on 127.0.0.1 only: the services reach it, the internet never does. It has no paths.
  • A redeploy never restarts it, unless its image or port changed, and never touches its data.
  • A changed port also needs a new URL: the deploy makes one (a new password) and restarts the box once. If the box already has its own variable env holding something else (another database's URL), the deploy refuses rather than overwrite it: name another env, or remove that variable.
  • The data is in /mnt/<volume>/<service> in the box. If the volume is not mounted, the database refuses to start rather than make an empty one on the box's own disk.
  • Renaming the service does not move its data: the deploy refuses while the volume holds the old name's database and says how to move it.
  • A new box given the same volume (remove the box, deploy again) gets the database it holds, when it is on the volume's host. Through kvbox.sh a new box may land on another host; the deploy then refuses and says so, since a volume's data cannot move yet.
  • psql in the box: kvbox exec --tty shop -- docker exec -it kvbox-db psql -U kvbox app.

Back the volume up on its own. Neither kvbox vm backup nor a snapshot copies a volume; kvbox volume backup create <volume> does, while the database keeps running, and kvbox volume backup restore <volume> <id> puts it back (Backups). Nothing is scheduled until you turn it on: kvbox volume backup schedule <volume> --on backs it up every night.

Redeploy

kvbox deploy                # every service
kvbox deploy --only api     # one service; repeat --only for more

The routes always come from the whole file, so --only on a box that never got some service warns that its paths answer nothing until that service is deployed.

Every service is built first, so a failed build deploys nothing. A service's release command runs next, and a failed one leaves that service as it was. Each one is then staged next to its running version and swapped in: static files without a gap, a server with the few seconds it takes to start.

In the box

Each service has its own directory and, when it has a server, its own systemd service:

kvbox exec shop -- journalctl -u kvbox-api -n 100     # the api service's log
kvbox exec shop -- ls /srv/app                         # web/  api/

They start again after a stop and start of the box.

Environment variables set for the box are shared by every service with a server. To apply a new or changed one:

kvbox env set NAME value --box shop
kvbox vm restart shop                  # the services start with the new values

A box made before base image v0.13.0 also needs a kvbox deploy after the restart: its services only pick up new values when they are deployed again. A stack's database is never restarted for a variable.

Limits today

  • A service you remove from kvbox.json keeps running in the box. Stop it there: kvbox exec shop -- sudo systemctl disable --now kvbox-<name>.
  • One box runs one stack. Deploying a stack to a box that held a single app replaces that app.

On this page