# Deploying

Source: https://bogie-go.com/docs/deploying.html

## One image, every role

The Dockerfile builds one binary that serves, works the queue (apps made with `--jobs`) and moves the schema, so a web container and a job container are always the same artifact, never built from different commits:

```
FROM golang:1.27-alpine AS builder
...
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/blog .

FROM alpine:3.21
RUN apk add --no-cache ca-certificates tzdata && adduser -D -u 10001 blog
USER blog

COPY --from=builder /src/config /app/config
COPY --from=builder /out/blog /usr/local/bin/blog
EXPOSE 8080
ENTRYPOINT ["blog"]
```

The encrypted credentials travel in the image; the keys never do. `.dockerignore` keeps every `*.key` out of the build context, so an image cannot carry both halves of the pair. With no arguments the image serves, reading `<NAME>_ENV` and `<NAME>_CREDENTIALS` from the environment; `migrate` is compiled in, so the same image also inspects and rolls back the schema:

```
docker exec <container> blog migrate status
docker exec <container> blog migrate down --yes
```

## Staging is the base, production is the overlay

`config/deploy.yml` **is** staging; a bare `kamal deploy` targets it:

```
kamal deploy
```

Production is reached only through `-d production`, which overlays `config/deploy.production.yml` on top:

```
kamal deploy -d production
```

Hashes in the overlay deep-merge with staging's; arrays (`env.secret`, `proxy.hosts`) replace wholesale rather than appending, which is why production restates `proxy.hosts` with its own domain but says nothing about `env.secret`: the list is identical, only the value Kamal substitutes differs.

### Three things to fill in before the first deploy

- **The server.** `servers.web` in `config/deploy.yml`, marked `CHANGE-ME.example.com`.
- **The host name.** `proxy.hosts`, marked the same way, staging and production each with their own.
- **The registry.** `image: CHANGE-ME/blog` and `registry.username`.

Kamal's healthcheck points at `/healthz` rather than its own default `/up`, since this service has no such route and `/healthz` touches nothing and allocates nothing.

## .kamal/secrets

What Kamal substitutes into the `secret:` lists in `deploy.yml`. Everything else lives in the encrypted credentials; only the decryption key stays on disk, gitignored, copied between machines by hand:

```
BLOG_MASTER_KEY=$(cat config/credentials/staging.key)
KAMAL_REGISTRY_PASSWORD=$(go tool credentials get -e staging kamal.registry_password)
```

`.kamal/secrets.production` is the same shape, reading `config/credentials/production.key` instead; Kamal picks whichever file matches the destination. `make credentials ENV=staging` makes the credentials file and its key the first time, before the first deploy to that environment can work; the service refuses to boot without a database URL in it.

## Migrate at boot

On by default: the container applies pending migrations before it starts serving, so the database only has to exist, not already match the code. `DATABASE_MIGRATE_AT_BOOT=false` (see [Configuration](https://bogie-go.com/docs/configuration.html)) turns it off, for a deployment that migrates from one place on purpose.

## The job role

Apps made with `--jobs` get a `job` role in both `deploy.yml` and the production overlay: the same image, started with the argument `worker` instead of serving. Same host as `web` until the queue needs one of its own.

```
  job:
    hosts:
      - CHANGE-ME.example.com
    cmd: worker
```

`cmd` is only the argument, `worker`, not `blog worker`: the image's `ENTRYPOINT` is already `blog`, so `cmd: blog worker` runs `blog blog worker`, which is not a command, so the container prints `unknown command "blog"; serving instead` and serves HTTP where the worker should be working. Apps made before v0.6.6 (`deploy.yml`) or v0.6.7 (the production overlay) have the old line; see [Moving an app from v0.5.5 to v0.6.8](https://bogie-go.com/docs/upgrading.html#v0-6).

### Running a command in a container

`deploy.yml` ends with three Kamal aliases, and its comments say how `app exec` treats the binary name. With `--reuse` it runs in the container that is up, so it takes the whole command, `kamal app exec --reuse "blog db seed"`. Without it Kamal starts a new container through the `ENTRYPOINT`, so leave the binary off: `kamal app exec "db seed"`.

```
aliases:
  logs: app logs -f
  shell: app exec --interactive --reuse "sh"
  status: app exec --reuse -r web "wget -qO- http://localhost:8080/healthz"
```

`kamal status` asks the web role only (`-r web`, since v0.6.7): a job container serves no HTTP, so asking it would fail.

### Migrations and tasks in production

By default there is nothing to run: the container migrates at boot (see [Migrate at boot](https://bogie-go.com/docs/deploying.html#migrate-at-boot)). To check and apply by hand, run the image's own commands in the container that is up. The outputs below come from a generated app, run with the environment `deploy.yml` sets, in a container started with `DATABASE_MIGRATE_AT_BOOT=false`. The commands were run inside that container, not through Kamal against a server, but they are the ones `deploy.yml` documents. For production add `-d production`; `-r web` limits a command to one role (Kamal: "Run commands on these roles instead of all").

```
$ kamal app exec --reuse "blog migrate status"
state       applied at              migration
applied     2026-11-05 02:27:23     20261105022613_create_posts.sql
pending     pending                 20261105022940_add_slug_to_posts.sql
```

```
$ kamal app exec --reuse "blog migrate up"
{"time":"2026-11-05T02:30:24.013645796Z","level":"INFO","msg":"schema applied","service":"blog","from":20261105022613,"to":20261105022940}
```

Afterwards `migrate status` shows both rows as `applied` and `blog migrate version` prints `20261105022940`.

To roll back the most recent migration, run `kamal app exec --reuse "blog migrate down --yes"`, which logs `"msg":"schema rolled back"`. Migrations drop tables on the way down, and there is no undo. Without `--yes` it refuses.

**pass --yes to confirm** is how `migrate down` refuses without the flag:

```
blog: migrate: down drops the most recent migration's tables; pass --yes to confirm
```

A one-off task has the same shape: `kamal app exec --reuse "blog db seed"` prints nothing and exits 0 when the seeds succeed. Every seed runs again each time.

Without `--reuse`, Kamal starts a new container through the `ENTRYPOINT`, so leave the binary off: `kamal app exec "migrate status"` (the same arguments, run with `docker run`, printed the status table). A common mistake is to keep the name off while using `--reuse`, as in `kamal app exec --reuse "migrate status"`. With `--reuse` the command runs inside the running container, which has no program called `migrate`, and Docker's message for that is the block below (captured with `docker exec`).

**executable file not found in $PATH** is Docker's message when the command names a program the container does not have:

```
OCI runtime exec failed: exec failed: unable to start container process: exec: "migrate": executable file not found in $PATH
```

With `--reuse` the binary name is required: `"blog migrate status"`.

## The database

Not an accessory by default. The premise is a Postgres server you already run, so `database.url` in the credentials points at that server, as a database of this service's own. Uncommenting an `accessories.db` block in `deploy.yml` gives the service its own Postgres container instead, resolved by container name on Kamal's network; the schema is still applied by the container itself at boot.
