Follow these steps when you want to run the Next.js app with npm on your host machine while PostgreSQL runs inside Docker. The flow keeps external services identical to production but lets you iterate with hot reload locally.
- Docker Desktop (or any Docker runtime) running.
- Node.js 20+ and npm installed on your host.
- An
.env.localfile inapp/that matches your deployment secrets.
cd app
LOCAL_DB_PORT=5432 docker compose up -d postgres- Change
LOCAL_DB_PORTif port5432is already occupied on your host. - Data persists inside the named
postgres_datavolume. Usedocker compose down -vif you need a clean slate.
- Copy the template:
cp app/.env.local.example app/.env.local. - Update secrets:
DATABASE_URLshould target the mapped host port, e.g.postgresql://dibgifts:password@localhost:5432/dibgifts_db.NEXTAUTH_SECRETmust be a 32+ character random string.- Replace the SMTP block with valid credentials (or point it to a dev SMTP relay like Mailtrap).
- Provide a real
NEXT_PUBLIC_UNSPLASH_ACCESS_KEYif you need banner search.
If you run a remote/deployed Postgres instance instead of the local container, just swap
localhost:5432with that hostname/port.
cd app
npm install
npm run migration:up # applies MikroORM migrations to the database
npm run db:init # optional helper that seeds auth tables- Run
npm run migration:upevery time the entity schema changes. - If you connect to an already-seeded production database, skip the init scripts to avoid clobbering data.
cd app
npm run dev- The app listens on
http://localhost:3000by default. Override withPORTinside.env.localif necessary. - The API layer (tRPC + NextAuth) now talks to the Dockerized Postgres instance through the shared
DATABASE_URL.
- Stop the app with
Ctrl+Cin the dev terminal. - Stop Postgres when you are done:
cd app && docker compose down. - Use
docker compose logs -f postgresif you need to inspect database output.
- Connection refused: Ensure the container is healthy (
docker ps+ check theSTATUS). Confirm the host port matchesDATABASE_URL. - SSL or timezone warnings: Add
?sslmode=disableor other parameters directly toDATABASE_URLif your remote DB requires them. - Migrations not applying: Verify the
.env.localfile exists before running MikroORM CLI commands; they read from the same environment variables.
With this setup you can iterate locally using npm while keeping the database layer isolated inside Docker (or any external Postgres instance) without running the whole stack in containers.