Skip to content

Self-host an identity provider with Docker Compose

Run Ostiary, the open-source identity provider, on your own server with Docker Compose: Postgres, migrations, reverse proxy, client IP, backups, upgrades.

Published on 7 min read

A self-hosted identity provider is only self-hosted if you can run it on a machine you control, without a platform doing half the work. Ostiary, the open-source identity provider built on Better Auth, ships a Dockerfile and a docker-compose.yml for exactly that: one command starts Postgres, applies the migrations, and runs the sign-in app, the admin console and a daily housekeeping job. This post walks through the setup, then the parts that matter in production: the reverse proxy, the client IP, backups and upgrades.

If you would rather not run a server at all, the Vercel and Neon deployment guide covers the managed path. If you are still deciding whether to run your own identity provider, start with Ostiary as a self-hosted Auth0 alternative.

What runs in the stack

docker-compose.yml defines five services on one machine:

ServiceWhat it does
dbPostgres 17 (postgres:17-alpine), with its data in the db-data volume
migrateApplies new database migrations and registers the APIs listed in OAUTH_API_AUDIENCES, then exits
authThe sign-in pages, the OpenID Connect provider and the account dashboard, on port 3000
adminThe admin console, on port 3001
cronCalls the daily housekeeping route at 04:00 UTC

The order is enforced by Docker Compose itself: migrate waits until Postgres answers pg_isready, and both apps wait until migrate has completed successfully. A failed migration therefore never leaves an app running against a half-migrated database.

What is inside the images

The Dockerfile builds one image per app (--target auth, --target admin) and one for the migrations (--target migrate). The app images start from node:22-alpine and hold only Next.js's standalone server: no package manager, and the process runs as the unprivileged node user. Each has a health check that calls GET /api/health every 30 seconds.

Before you start

You need:

  • a server with Docker and Docker Compose;
  • two hostnames pointing at it, subdomains of one domain, such as auth.example.com and admin.example.com;
  • a reverse proxy that terminates TLS (examples below);
  • a Resend API key and a sender on a domain verified in Resend, for verification emails, password resets and sign-in codes. Production refuses to start without them.

Install in four commands

Clone the repository and copy the example environment file:

git clone https://github.com/florianamette/ostiary.git && cd ostiary
cp .env.docker.example .env

Then fill in .env:

VariableValue
AUTH_APP_URL, ADMIN_APP_URLThe public HTTPS addresses, such as https://auth.example.com and https://admin.example.com
COOKIE_DOMAINThe parent domain of both, such as .example.com (see below)
POSTGRES_PASSWORDopenssl rand -base64 24
BETTER_AUTH_SECRETopenssl rand -base64 32
CRON_SECRETopenssl rand -base64 24
RESEND_API_KEY, RESEND_FROMYour Resend key and sender, such as "Ostiary <auth@example.com>"
ADMIN_EMAILSYour email address
IP_ADDRESS_HEADERSx-real-ip, the default in the example file

Compose refuses to start while a required value is missing, with a message such as set BETTER_AUTH_SECRET in .env, so a forgotten secret fails loudly instead of silently. Build and start everything, then check that migrate exited and the apps are healthy:

docker compose up -d --build
docker compose ps -a

Build-time settings

Most variables are read when a container starts. Three are built into the apps' browser code and Content Security Policy, so Compose passes them as build arguments: NEXT_PUBLIC_APP_URL and NEXT_PUBLIC_ADMIN_APP_URL (from AUTH_APP_URL and ADMIN_APP_URL) and CAPTCHA_PROVIDER. After changing one of them, rebuild with docker compose up -d --build.

Variables beyond the defaults

The compose file passes a fixed list of variables to the containers, in its x-app-env block. To use another one from the configuration reference, such as GITHUB_CLIENT_ID for GitHub sign-in, add it to that block as NAME: ${NAME} and set it in .env. Most other settings, such as social sign-in providers, signing keys and webhooks, live in the admin console and need no rebuild.

Put a reverse proxy in front

Each hostname goes to its app: auth.example.com to port 3000, admin.example.com to port 3001. With Caddy, which obtains TLS certificates by itself:

auth.example.com {
	reverse_proxy localhost:3000 {
		header_up X-Real-IP {remote_host}
	}
}

admin.example.com {
	reverse_proxy localhost:3001 {
		header_up X-Real-IP {remote_host}
	}
}

With nginx, set proxy_set_header X-Real-IP $remote_addr; in each location, and raise client_max_body_size to 11m on the admin console: nginx refuses bodies over 1 MB by default, and user imports upload files of up to 10 MB. The self-hosting guide has the complete nginx configuration.

Do not expose ports 3000 and 3001 to the internet: a client that reaches an app directly can send any header, including a fake client IP. When the proxy runs on the same machine, bind them to localhost with AUTH_PORT=127.0.0.1:3000 and ADMIN_PORT=127.0.0.1:3001 in .env.

Tell Ostiary the client IP

Rate limits, sessions, the audit log and the sign-in history all record the visitor's address. Off Vercel, Ostiary trusts no header by default, because anyone can write an x-forwarded-for header (RFC 7239 describes the standard Forwarded header and the same caveat). Until you configure it, every client shares one rate-limit counter per endpoint, no IP is recorded, and the apps log a warning at startup.

  • Behind the Caddy or nginx examples above: IP_ADDRESS_HEADERS=x-real-ip.
  • Behind Cloudflare, with the origin reachable only through it: IP_ADDRESS_HEADERS=cf-connecting-ip.
  • Behind proxies that append to x-forwarded-for: TRUSTED_PROXIES= their IPs or CIDR ranges.

Only name a header your proxy overwrites. The Client IP section of the security guide explains each case.

Sign in as the first admin

Open AUTH_APP_URL, sign up with the address in ADMIN_EMAILS and open the verification link sent to it: the account becomes an admin once the address is verified. Then open ADMIN_APP_URL. The admin console asks you to turn on two-factor authentication first, since the compose file sets REQUIRE_ADMIN_2FA to true unless you change it. A passkey is a good second factor; the passkeys post explains how they work in Ostiary.

One session for both apps

The admin console has no sign-in page of its own: it sends you to the auth app, then reads the session cookie. Browsers only send that cookie to the console when both apps share a parent domain and COOKIE_DOMAIN names it. The cookie then reaches every host under that domain, so read the COOKIE_DOMAIN section first, and prefer a domain dedicated to identity, such as auth.example.com and admin.auth.example.com with COOKIE_DOMAIN=.auth.example.com.

Keep it running

Housekeeping

The cron service calls GET /api/cron/webhooks on the auth app once a day with Authorization: Bearer $CRON_SECRET. That route retries due webhook deliveries, deletes deliveries older than 30 days, deletes sign-in events older than AUTH_EVENT_RETENTION_DAYS (90 by default), and applies the Inactive accounts rule when it is on. To use a systemd timer or a Kubernetes CronJob instead, remove the service and call the route the same way.

Backups

Back up two things: the database and .env. BETTER_AUTH_SECRET encrypts the stored signing keys, social provider secrets and webhook secrets, so a database restored with another secret cannot use them. A dump in Postgres's custom format, with pg_dump:

docker compose exec -T db pg_dump -U ostiary -Fc ostiary > ostiary-$(date +%F).dump

To restore, stop auth, admin and cron, run pg_restore --clean --if-exists into the db service, and start everything again. The self-hosting guide has the exact commands. Test a restore before you need one.

Upgrades

Read the changelog for the versions you skip, back up the database, then pull and rebuild:

git pull
docker compose up -d --build

migrate runs again before the apps start and applies only the new migrations. If it fails, the apps do not start, and docker compose logs migrate says why.

Monitoring

Both apps answer GET /api/health: 200 with {"status":"ok"} when the app and its database respond within 3 seconds, 503 otherwise. It is public and reveals nothing else, so you can point an uptime monitor or a load balancer at it. Logs go to standard output: docker compose logs -f auth admin.

Without Compose

The same images run on any orchestrator. Build them with your domains as build arguments, give the three containers the same environment, run the migrate image to completion before starting or upgrading the apps, and schedule the housekeeping call. The apps also run without Docker, on Node.js 20.9 or later. Both paths are in the self-hosting guide.

Next steps

Once the stack is up, register your first application under Applications and point it at https://auth.example.com/api/auth/.well-known/openid-configuration; any OIDC library works, with PKCE for the authorization code flow. To let your own APIs accept tokens from Ostiary, read how to protect an API with JWT access tokens. Or compare the hosting options on the home page first.

Related articles

Deploy Ostiary, the open-source identity provider, on Vercel with Neon Postgres: environment variables, the first admin, the admin console, local setup.
Move users from Auth0 to Ostiary with a dry run first. Imported bcrypt hashes keep working and become scrypt at sign-in, with no password reset.
How Ostiary tells your apps when users change: user and membership webhook events, Standard Webhooks signatures, retries, and a receiver to copy.