# Deploy with Docker

Run Charter and Postgres with Docker Compose.

You may run the Charter container image and a Postgres database with Docker Compose. The image entrypoint is the `charter` binary; the default command is `charter serve`.

> [!INFO]
> Prefer a binary on the host with systemd? See [Install on Ubuntu](/docs/install-on-ubuntu/). For Cloudflare Authenticated Origin Pulls, continue with [Mutual TLS with Cloudflare](/docs/mutual-tls-cloudflare/).

## What you need

- [Docker Engine](https://docs.docker.com/engine/install/)
- [Docker Compose](https://docs.docker.com/compose/install/) (the `docker compose` plugin)
- A Postgres database Charter can own (this Compose file includes one). Role privileges and `DATABASE_URL` shape: [Requirements](/docs/requirements/).

Confirm both:

```sh
docker --version
docker compose version
```

## Step 1: Create a Compose file

In an empty directory, create `compose.yaml`. You should pin the image to a release from [Install](/docs/install/). Prerelease tags use the exact version only; `latest` tracks stable releases.

```yaml
services:
  postgres:
    image: postgres:17-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: charter
      POSTGRES_PASSWORD: "change-me"
      POSTGRES_DB: charter
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U charter -d charter"]
      interval: 5s
      timeout: 5s
      retries: 10

  charter:
    image: ghcr.io/snaapi/charter:1.0.0-alpha.5
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "3000:3000"
    environment:
      APP_ENV: production
      DATABASE_URL: "postgres://charter:change-me@postgres:5432/charter"
      COOKIE_SIGNING_KEY: "replace-with-openssl-rand-base64-64"
      CHARTER_BASE_URL: "http://localhost:3000"
      CHARTER_API_NAME: "Charter API"

volumes:
  postgres_data:
```

Replace `change-me` with a strong Postgres password in both `POSTGRES_PASSWORD` and `DATABASE_URL`. Quote the values so characters like `/` in a password or signing key stay literal. Generate a cookie signing key:

```sh
openssl rand -base64 64
```

Paste the output into `COOKIE_SIGNING_KEY`. Keep that value out of version control. Changing it signs every existing session out.

`charter serve` binds `0.0.0.0:3000` by default, so Compose only needs to publish port `3000`. The image includes a `HEALTHCHECK` that runs `charter health` against `DATABASE_URL`.

> [!WARNING]
> Do not publish Postgres to the host in production. The example above leaves Postgres on the Compose network only. The credentials in this file are placeholders; treat them like secrets.

## Step 2: Start Charter

```sh
docker compose up -d
```

Check that both containers are running:

```sh
docker compose ps
```

You should see `postgres` healthy and `charter` up, with `0.0.0.0:3000->3000/tcp`.

Probe the API listener:

```sh
curl -sS http://localhost:3000/healthz
# ok

curl -sS http://localhost:3000/readyz
# ready
```

`/healthz` does not touch the database. `/readyz` pings Postgres and returns `503` when it cannot answer.

## Apply your config

The schema is not baked into the image. After the stack is up, you may import a config the same way you would on a host binary. From a machine that can reach the database URL in Compose, or by running a one-off container on the same network:

```sh
docker compose run --rm \
  -v "$PWD":/work -w /work \
  --user "$(id -u):$(id -g)" \
  charter \
  import config --path charter.toml --apply
```

Omit `--apply` first for a dry run. See [Quickstart](/docs/quickstart/) and [Config file](/docs/config-file/) for the file format.

Use `charter serve` in this stack, not `charter dev`. Override the container command only when you need another role, for example a dedicated worker:

```yaml
command: ["work"]
```

## Secure the deployment

`APP_ENV=production` (or `prod`) makes Charter refuse to start without your own `COOKIE_SIGNING_KEY`. That key signs session cookies. Never send it from a browser or agent client.

Also set:

- `CHARTER_BASE_URL` to the public URL callers use (for example `https://api.example.com`). Charter builds OAuth redirects from it and trusts it as an origin.
- A unique Postgres password, not the placeholder above.
- `CHARTER_TRUSTED_PROXIES` if a reverse proxy sits in front and you want rate limits to use `X-Forwarded-For`. See [Environment variables](/docs/environment-variables/).

Without TLS flags, `charter serve` speaks plain HTTP, which is what you want behind a proxy or load balancer that terminates TLS. For Authenticated Origin Pulls on a Virtual Private Server (VPS), follow [Mutual TLS with Cloudflare](/docs/mutual-tls-cloudflare/).

To create the first operator without a shell in the container, set `CHARTER_SETUP_TOKEN` to the output of `openssl rand -base64 32` and open `http://localhost:3000/setup`. The secret has to be at least 32 characters. Enter it with a name and email. Charter shows an API key once, and that secret will not work again. See [Environment variables](/docs/environment-variables/).

## Server logs

Follow Charter logs:

```sh
docker compose logs -f charter
```

Or by container id from `docker ps`:

```sh
docker logs <container-id>
```

See [Docker's logging docs](https://docs.docker.com/config/containers/logging/) for drivers and retention.

## Update Charter

1. Pick a newer image tag from [Install](/docs/install/).
2. Change the `image:` line under `charter` in `compose.yaml`.
3. Pull and recreate:

```sh
docker compose pull charter
docker compose up -d
```

Postgres data stays in the `postgres_data` volume across recreates.

## Docker networking

Hostnames differ by how containers are started. Use these when Charter calls other HTTP APIs (for example webhooks), or when other services call Charter.

### Linux

| Connection | Address | Notes |
| --- | --- | --- |
| Charter to an API on the host | `172.17.0.1:3001` (typical Docker bridge), or use `network_mode: host` and `localhost:3001` | Confirm the bridge IP with `ip -4 addr show docker0` |
| Host process to Charter | `localhost:3000` | Matches the published port |
| Charter to another Compose service | Service name, for example `api:3001` | Same `compose.yaml` network |
| Another Compose service to Charter | `charter:3000` | Service name from this guide |
| Separate `docker run` containers | Container IP from `docker inspect` | Prefer one Compose project and service names instead |

### macOS and Windows

| Connection | Address | Notes |
| --- | --- | --- |
| Charter to an API on the host | `host.docker.internal:3001` | Docker Desktop resolves this name |
| Host process to Charter | `localhost:3000` | Matches the published port |
| Charter to another Compose service | Service name, for example `api:3001` | Same `compose.yaml` network |
| Another Compose service to Charter | `charter:3000` | Service name from this guide |

Inside Compose, `DATABASE_URL` must use the Postgres service name (`postgres`), not `localhost`. `localhost` inside the Charter container is the container itself.
