Deploying with Docker Compose
Learn how to self-host your Better-T-Stack app with the generated Dockerfiles and docker-compose.yml
This guide explains how Better-T-Stack packages your applications for self-hosting with Docker Compose. You'll learn:
Choosing docker as a deploy target generates:
docker-compose.yml at the repo root defines the web, server, and (optionally) database services with health checks and startup ordering.apps/*/Dockerfile are multi-stage builds that install the whole monorepo workspace, build one app, and ship a minimal runtime image.nginx.conf is generated for the static TanStack Router SPA, which is served by nginx. Solid runs as a Node SSR server from its Nitro .output build.Combined deployment (web + server + database):
npm create better-t-stack@latest my-app \
--frontend tanstack-router \
--backend hono \
--runtime bun \
--database postgres \
--db-setup docker \
--web-deploy docker \
--server-deploy dockerWeb-only (fullstack self backend):
npm create better-t-stack@latest my-app \
--frontend next \
--backend self \
--web-deploy dockerServer-only:
npm create better-t-stack@latest my-app \
--frontend none \
--backend fastify \
--runtime node \
--server-deploy dockerServer deployment requires the bun or node runtime. Adding --db-setup docker puts a Postgres, MySQL, or MongoDB container in the same compose file with a named volume and health check.
bun docker:build # docker compose build
bun docker:up # docker compose up -d --build
bun docker:logs # docker compose logs -f
bun docker:down # docker compose downAfter docker:up:
| Service | URL | Notes |
|---|---|---|
| web | http://localhost:3001 | nginx (SPA frontends) or the framework's server |
| server | http://localhost:3000 | health-checked; web waits for it to become healthy |
| database | localhost:5432 / 3306 / 27017 | only with --db-setup docker |
Startup ordering is handled with depends_on + condition: service_healthy: the database must pass its health check before the server starts, and the server must pass its fetch('http://localhost:3000/') health check before the web container starts.
With --db-setup docker you also get scoped database scripts for local development without the full stack:
bun db:start # docker compose up -d postgres (just the database, detached)
bun db:watch # docker compose up postgres (with logs in the foreground)Each Dockerfile is a multi-stage build with the monorepo root as build context:
varlock/auto-load with Node. SPA frontends use nginx; SSR frontends and servers use Bun or Node (current LTS) images and run as the image's unprivileged node or bun user.Database services use postgres:18, mysql:9.7 (the current MySQL LTS), and mongo:9.
Three rules cover everything:
VITE_SERVER_URL are compose build args — they're inlined into the client bundle when the image builds. The generated default is http://localhost:3000 (the published server port). Changing them requires an image rebuild, not just a restart..env file. Compose loads app values through env_file. The same files are required as build-secret sources when building images; Varlock rejects missing required values.localhost — so compose sets values like DATABASE_URL: postgresql://postgres:...@postgres:5432/my-app and CORS_ORIGIN: http://localhost:3001 in the environment: block, which wins over env_file. Your local .env files stay pointed at localhost for non-Docker development.BuildKit keeps the mounted env files out of normal COPY layers. Frameworks that embed resolved server configuration, including Next, can still put those values in server build artifacts. See Varlock encrypted deployments for encrypting that configuration with a key supplied at both build and runtime.
Database passwords default to password and can be overridden with compose variables (POSTGRES_PASSWORD, MYSQL_PASSWORD, MONGO_PASSWORD) — set them in a root .env file or export them before docker:up.
The generated compose file is production-shaped (multi-stage images, health checks, restart: unless-stopped, named volumes), but a few things are sized for localhost:
CORS_ORIGIN / BETTER_AUTH_URL on the server to your real web origin.environment:, an .env file on the host, or your orchestrator's secret store).<project>_postgres_data etc.); docker compose down -v deletes it.The stack publishes 3001 (web), 3000 (server), and the database port. Stop whatever is bound to them, or edit the ports: mappings in docker-compose.yml.
Public variables are build args baked into the client bundle. Rebuild the image: bun docker:up (it passes --build) or bun docker:build first.
Inside compose, the database host is the service name (postgres, mysql, mongodb), not localhost. Use the compose-provided DATABASE_URL (already set in the environment: block) instead of the one in apps/server/.env.
It waits for the server health check. Check bun docker:logs — if the server is crash-looping (usually missing env vars or a bad DATABASE_URL), the web container stays in waiting state.
The compose file sets CORS_ORIGIN: http://localhost:3001 when the web service is present. If you access the app from a different host or port, update that value to match the origin you're actually using and restart.