This guide takes a Korr program from your machine to a server: building one
executable, configuring it, running it as a service, putting HTTPS in front,
scaling, Docker, logs and updates. Examples use a web app made with
korr new myapp, but everything except the proxy part applies to CLI tools too.
cd myapp
korr test # make sure the tests pass
korr build main.krr -o myapp # creates ./myapp (Windows: myapp.exe)
korr build compiles every .krr file your program imports (including
packages from korr_modules/) and appends them to a copy of the
korr runtime. The result:
Not inside the executable: files your program reads at run time: public/
(static files), .env, uploaded files, a SQLite database. Keep them next to the
executable. Inside a built program, __dir__ is the executable’s folder, so
app.static("/public", "{__dir__}/public") works wherever the program is started
from.
/srv/myapp/
myapp # the executable
public/ # static files
.env # configuration (optional)
The executable contains the runtime it was built with, so build on the same
operating system and CPU as the server, or pass another platform’s korr with
--runtime. Release archives contain exactly that runtime:
# On a Mac, build a Linux server binary:
curl -fsSLO https://github.com/refkinscallv/korr/releases/latest/download/korr-linux-x86_64.tar.gz
tar -xzf korr-linux-x86_64.tar.gz
korr build main.krr --runtime ./korr -o myapp-linux
Which Linux runtime to use:
korr-linux-*.tar.gz (glibc): the fastest; needs glibc 2.34 or newer
(Ubuntu 22.04+, Debian 12+, RHEL 9+).korr-linux-*-musl.tar.gz (static): runs on any distribution, including
Alpine and older releases, but code that allocates a lot (strings, maps,
objects) runs up to about 1.7x slower because of musl’s memory allocator.
Loops and arithmetic run at the same speed.Read settings from environment variables, with defaults for development:
// no-check
os.load_env("{__dir__}/.env") // optional; real env vars win
let port = int(os.env("PORT", "8080"))
let conn = db.open(os.env("DATABASE_URL", "sqlite:{__dir__}/app.db"))
let secret = os.env("SECRET_KEY")
if secret == null
throw error("SECRET_KEY is not set")
.env out of Git and readable only by the service
user (chmod 600 .env).os.env values are strings: convert with int(...), compare with == "true".# /etc/systemd/system/myapp.service
[Unit]
Description=myapp (Korr)
After=network.target
[Service]
User=myapp
WorkingDirectory=/srv/myapp
ExecStart=/srv/myapp/myapp
Environment=PORT=8080
EnvironmentFile=-/srv/myapp/.env
Restart=always
RestartSec=2
# Hardening: the app can only write its own folder
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
ReadWritePaths=/srv/myapp
[Install]
WantedBy=multi-user.target
sudo useradd --system --home /srv/myapp --shell /usr/sbin/nologin myapp
sudo chown -R myapp:myapp /srv/myapp
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp
journalctl -u myapp -f # logs
<!-- ~/Library/LaunchAgents/com.example.myapp.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>com.example.myapp</string>
<key>ProgramArguments</key><array><string>/Users/me/myapp/myapp</string></array>
<key>EnvironmentVariables</key><dict><key>PORT</key><string>8080</string></dict>
<key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>
<key>StandardOutPath</key><string>/Users/me/myapp/myapp.log</string>
<key>StandardErrorPath</key><string>/Users/me/myapp/myapp.log</string>
</dict>
</plist>
launchctl load ~/Library/LaunchAgents/com.example.myapp.plist
Windows services need a wrapper; NSSM and WinSW both work:
nssm install myapp C:\apps\myapp\myapp.exe
nssm set myapp AppDirectory C:\apps\myapp
nssm set myapp AppEnvironmentExtra PORT=8080
nssm set myapp AppStdout C:\apps\myapp\myapp.log
nssm set myapp AppStderr C:\apps\myapp\myapp.log
nssm start myapp
The built-in server speaks plain HTTP. In production, always put a reverse proxy in front of it. The proxy:
Make the app listen on 127.0.0.1 only (the default for app.listen(port)), so it
is reachable only through the proxy.
# /etc/caddy/Caddyfile
example.com {
encode gzip
reverse_proxy 127.0.0.1:8080
}
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
client_max_body_size 10m; # match app.limit(...)
gzip on;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 30s;
}
# WebSockets (app.ws): pass the upgrade on; the server pings every 15 s
location /chat {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 60s;
}
}
Behind a proxy, call app.trust_proxy(true) so that req.ip and
rate limiting see the client’s address instead of the
proxy’s (both Nginx with X-Forwarded-For above and Caddy set the header).
Caddy passes WebSockets and Server-Sent Events on without extra settings. Keep
the Host header (proxy_set_header Host $host): Korr compares it with the
browser’s Origin before accepting a WebSocket.
Once HTTPS works, create cookies with secure: true
(http.cookie("sid", token, { secure: true })).
On one machine, use workers: one Korr process serves several requests at once, one per worker thread (how workers work).
let app = http.app()
app.listen(int(os.env("PORT") ?? "8080"), { workers: "auto" }) // one per CPU thread
or set KORR_WORKERS=auto (or a number) in the service’s environment without
changing the code. Keep shared data in the database, not in variables: each
worker has its own copy of the program.
You can also run several processes, for example to restart them one at a time without downtime, or to use several machines, and let the proxy balance between them:
# /etc/systemd/system/myapp@.service: one instance per port
[Service]
User=myapp
WorkingDirectory=/srv/myapp
ExecStart=/srv/myapp/myapp
Environment=PORT=%i
EnvironmentFile=-/srv/myapp/.env
Restart=always
sudo systemctl enable --now myapp@8081 myapp@8082 myapp@8083 myapp@8084
upstream myapp {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
server 127.0.0.1:8083;
server 127.0.0.1:8084;
}
# in the server block: proxy_pass http://myapp;
Caddy: reverse_proxy 127.0.0.1:8081 127.0.0.1:8082 127.0.0.1:8083 127.0.0.1:8084.
With several instances, keep all shared state in the database: a map in one process (for example in-memory sessions) is not visible to the others. SQLite works with several processes on one machine (Korr waits up to 5 seconds for a lock); for heavier write loads or several machines use PostgreSQL or MySQL.
The official image, ghcr.io/refkinscallv/korr (linux/amd64 and linux/arm64), has
the korr command on a small Alpine base and runs as an unprivileged user:
docker run --rm ghcr.io/refkinscallv/korr version
docker run --rm -v "$PWD:/app" -p 8080:8080 ghcr.io/refkinscallv/korr main.krr
Tags: an exact version (0.13.0), the latest patch of a minor version (0.13),
and latest. For your app, start from it:
# Dockerfile
FROM ghcr.io/refkinscallv/korr:0.13
COPY --chown=65534:65534 . /app
RUN ["korr", "test"]
ENV PORT=8080
EXPOSE 8080
CMD ["main.krr"]
IN_DOCKER=1 is set in the image, so os.env("IN_DOCKER") tells your program it
runs in a container. To build a single executable in a Debian image instead (no
Korr in the final image):
# Dockerfile
FROM debian:bookworm-slim AS build
ARG KORR_VERSION=latest
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates \
&& if [ "$KORR_VERSION" = latest ]; then base=https://github.com/refkinscallv/korr/releases/latest/download; \
else base=https://github.com/refkinscallv/korr/releases/download/$KORR_VERSION; fi \
&& curl -fsSL "$base/korr-linux-$(uname -m).tar.gz" | tar -xz -C /usr/local/bin korr
WORKDIR /app
COPY . .
RUN korr test && korr build main.krr -o /app/server
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates \
&& rm -rf /var/lib/apt/lists/* && useradd --system --no-create-home app
WORKDIR /app
COPY --from=build /app/server /app/server
COPY --from=build /app/public /app/public
USER app
ENV PORT=8080
EXPOSE 8080
CMD ["/app/server"]
Inside a container the app must listen on all interfaces, so start the server with
app.listen(port, "0.0.0.0") (for example when os.env("IN_DOCKER") is set).
docker build -t myapp .
docker run -p 8080:8080 -e SECRET_KEY=... myapp
log.format("json"); to a file:
log.output("{__dir__}/app.log").// no-check
app.get("/health", fn(req) => { ok: true })
sqlite3 tool (sqlite3 app.db ".backup backup.db")
or by copying the file while the app is stopped.migrations/ and apply them at
start-up with conn.migrate("{__dir__}/migrations") (see
Migrations); ship the folder next to the executable.See Databases for the API.
korr build main.krr -o myapp.new
scp myapp.new server:/srv/myapp/
ssh server 'cd /srv/myapp && cp myapp myapp.prev && mv myapp.new myapp && sudo systemctl restart myapp'
# roll back: mv myapp.prev myapp && sudo systemctl restart myapp
Update Korr itself regularly (re-run the installer) and rebuild, so your executables include the latest fixes.
127.0.0.1chmod 600 .env, never in Gitsecure: true; req.csrf_field in every formvalidate(...); passwords hashed with crypto.hash_passwordf.filename in their own folder, with size and type checksapp.limit(...) and the proxy’s body limit/health endpoint monitoredkorr test passes before every deploySee also the Security model.
| Previous: Packages | Docs index | Next: Security model |