---
url: /runcobo/guide/deployment.md
description: >-
  Build a release binary, configure it with environment variables, migrate the
  database, and run Runcobo in production.
---

# Deployment

A Runcobo app compiles to a single executable. Templates are compiled in, and the `cobo` toolchain isn't needed at runtime. Deploying means building that executable, migrating the database, and running it with the right environment.

## 1. Build a release binary

```sh
shards install --production
crystal build src/main.cr --release --no-debug -o bin/server
```

`--release` turns on optimizations; build on the same OS and CPU architecture you deploy to. `shards install` also builds `bin/cobo`, which the [migration step](#_3-migrate-the-database) uses.

What the server needs at runtime:

| Ship | Why |
| --- | --- |
| `bin/server` | The application — templates, layouts and generated queries are inside it |
| `public/` | Only if you serve [static files](/guide/static-files) from it |
| Any directory you read or write at runtime | e.g. the target of [`render_file`](/guide/responses#files) or saved [uploads](/guide/uploads) |

What it does **not** need: `src/` (templates included), `lib/`, `db/queries/`, `sqlc.yaml`, `cobo` or sqlc.

### In a container

The Crystal team publishes Alpine-based images on Docker Hub (`crystallang/crystal:<version>-alpine`), which can link a fully static binary with `--static`. A two-stage build keeps the final image small:

```dockerfile [Dockerfile]
ARG CRYSTAL_VERSION=1.22.3

FROM crystallang/crystal:${CRYSTAL_VERSION}-alpine AS build
WORKDIR /app
COPY shard.yml shard.lock ./
RUN shards install --production --skip-postinstall --skip-executables   # the image doesn't need cobo
COPY src/ src/
RUN crystal build src/main.cr --release --no-debug --static -o bin/server

FROM alpine:3
RUN apk add --no-cache ca-certificates
WORKDIR /app
COPY --from=build /app/bin/server ./server
COPY public/ public/
EXPOSE 3000
CMD ["./server"]
```

## 2. Configure with environment variables

Runcobo reads these at startup:

| Variable | Default | Description |
| --- | --- | --- |
| `HOST` | `0.0.0.0` | Address to listen on |
| `PORT` | `3000` | Port to listen on |
| `REUSE_PORT` | `false` | `true` lets several processes share the port ([details](/guide/server#multiple-processes)). Any other value fails at startup. |
| `CERT`, `KEY` | — | Certificate chain and private key files to serve HTTPS directly. Set both or neither. |
| `SKIP_LOG` | — | `true` removes the access log and request IDs ([details](/guide/logging#turning-access-logs-off)) |

Your own code reads the rest. In the blog app, `main.cr` reads `DATABASE_URL`. If you call [`Log.setup_from_env`](/guide/logging#configuring-output), `LOG_LEVEL` sets the log level.

Arguments passed to `Runcobo.start` in code take precedence over these variables.

## 3. Migrate the database

Run migrations from your deploy job, before starting the new version. The job needs the `cobo` binary, `runcobo.yml` and `db/migrations/` from the release being deployed:

```sh
export PRODUCTION_DATABASE_URL="postgresql://app@db.internal:5432/blog"
bin/cobo db status production --exit-code    # optional: fail if anything is pending
bin/cobo db migrate production --wait --migration-lock-timeout 2m
```

* `cobo` connects with **`sslmode=verify-full`** unless the URL sets another `sslmode`.
* On PostgreSQL, an advisory lock stops two deploys from migrating at once. MySQL and SQLite have no migration lock, so run one deploy at a time; MySQL URLs get `tls=true` unless they set `tls`.
* Production can't be rolled back with `cobo`; ship a corrective migration instead.

Write migrations that the **running** version can live with — add columns before code uses them, drop them after it stops. See [Database & Migrations](/cli/database#staging-and-production) for all options.

## 4. Run the server

```sh
DATABASE_URL="postgresql://app@db.internal:5432/blog" PORT=8080 ./bin/server
```

### Health checks

Point your load balancer or orchestrator at `GET /healthcheck`. It returns `200` with the current time and doesn't touch the database.

### Stopping and restarting

On `SIGTERM` (or `SIGINT`), Runcobo stops accepting connections, answers new requests on open connections with `503`, and waits up to **30 seconds** for in-flight requests before exiting. Give your orchestrator a longer grace period — Kubernetes' default `terminationGracePeriodSeconds` is 30, for example, so set it to 35 or more. Change the wait with [`shutdown_timeout:`](/guide/server#graceful-shutdown).

### Behind a load balancer

If TLS terminates at a proxy, configure [trusted proxies](/guide/proxies) so `request.remote_ip`, `request.scheme` and `request.base_url` reflect the client, not the proxy:

```crystal
Runcobo.start(
  handlers: handlers,
  proxy_config: Runcobo::ProxyConfig.new(trusted_proxies: ["10.0.0.0/8"]),
)
```

### Logs

Logs go to standard output, one line per request with its request ID ([details](/guide/logging)). Let your platform collect stdout, and forward `X-Request-Id` from your load balancer to correlate its logs with yours.

## Checklist

* Built with `--release` for the target platform
* `public/` and any runtime directories shipped
* `DATABASE_URL` and other secrets set in the environment, not in files
* Migrations applied by the deploy job before the new version starts
* Health check on `/healthcheck`
* Shutdown grace period longer than `shutdown_timeout`
* Trusted proxies configured if TLS ends at a load balancer
