Make Docker Compose Coolify-ready

This commit is contained in:
2026-02-20 22:41:21 -05:00
parent 6e299b1e1f
commit 575bf91ecd
6 changed files with 48 additions and 15 deletions

View File

@@ -3,10 +3,18 @@ DATABASE_URL=postgres://postgres:postgres@localhost:5432/fiscal
POSTGRES_USER=postgres POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres POSTGRES_PASSWORD=postgres
POSTGRES_DB=fiscal POSTGRES_DB=fiscal
POSTGRES_HOST=localhost
# Backend # Backend
PORT=3001 PORT=3001
NODE_ENV=development NODE_ENV=development
JWT_SECRET=change-this-to-a-random-secret-key
BETTER_AUTH_SECRET=change-this-to-a-random-secret-key
BETTER_AUTH_BASE_URL=http://localhost:3001
GITHUB_ID=
GITHUB_SECRET=
GOOGLE_ID=
GOOGLE_SECRET=
# Frontend # Frontend
NEXT_PUBLIC_API_URL=http://localhost:3001 NEXT_PUBLIC_API_URL=http://localhost:3001

View File

@@ -23,6 +23,13 @@ POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password POSTGRES_PASSWORD=your_password
POSTGRES_DB=fiscal POSTGRES_DB=fiscal
PORT=3001 PORT=3001
BETTER_AUTH_SECRET=your-random-long-secret
BETTER_AUTH_BASE_URL=https://api.your-fiscal-domain.com
JWT_SECRET=your-jwt-secret-key-min-32-characters
GITHUB_ID=your-github-oauth-client-id
GITHUB_SECRET=your-github-oauth-client-secret
GOOGLE_ID=your-google-oauth-client-id
GOOGLE_SECRET=your-google-oauth-client-secret
NEXT_PUBLIC_API_URL=https://your-fiscal-domain.com NEXT_PUBLIC_API_URL=https://your-fiscal-domain.com
``` ```
@@ -63,10 +70,15 @@ NEXT_PUBLIC_API_URL=https://your-fiscal-domain.com
- `DATABASE_URL` - PostgreSQL connection string - `DATABASE_URL` - PostgreSQL connection string
- `PORT` - Server port (default: 3001) - `PORT` - Server port (default: 3001)
- `NODE_ENV` - Environment (development/production) - `NODE_ENV` - Environment (development/production)
- `BETTER_AUTH_SECRET` - Required in production; use a long random secret
- `BETTER_AUTH_BASE_URL` - Public backend URL used for auth callbacks
### Frontend ### Frontend
- `NEXT_PUBLIC_API_URL` - Backend API URL - `NEXT_PUBLIC_API_URL` - Backend API URL
`NEXT_PUBLIC_API_URL` is used at image build time in the frontend Docker build.
Set it in Coolify before deploying so the generated client bundle points to the correct backend URL.
## Database Setup ## Database Setup
The application will automatically create the database schema on startup. To manually run migrations: The application will automatically create the database schema on startup. To manually run migrations:

View File

@@ -1,9 +1,11 @@
import { betterAuth } from "better-auth"; import { betterAuth } from "better-auth";
import { Pool } from "pg"; import { Pool } from "pg";
const defaultDatabaseUrl = `postgres://${process.env.POSTGRES_USER || 'postgres'}:${process.env.POSTGRES_PASSWORD || 'postgres'}@${process.env.POSTGRES_HOST || 'localhost'}:5432/${process.env.POSTGRES_DB || 'fiscal'}`;
export const auth = betterAuth({ export const auth = betterAuth({
database: new Pool({ database: new Pool({
connectionString: process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost:5432/fiscal', connectionString: process.env.DATABASE_URL || defaultDatabaseUrl,
}), }),
emailAndPassword: { emailAndPassword: {
enabled: true, enabled: true,

View File

@@ -1,6 +1,8 @@
import postgres from 'postgres'; import postgres from 'postgres';
const sql = postgres(process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost:5432/fiscal', { const defaultDatabaseUrl = `postgres://${process.env.POSTGRES_USER || 'postgres'}:${process.env.POSTGRES_PASSWORD || 'postgres'}@${process.env.POSTGRES_HOST || 'localhost'}:5432/${process.env.POSTGRES_DB || 'fiscal'}`;
const sql = postgres(process.env.DATABASE_URL || defaultDatabaseUrl, {
max: 10, max: 10,
idle_timeout: 20, idle_timeout: 20,
connect_timeout: 10 connect_timeout: 10

View File

@@ -3,13 +3,13 @@ services:
image: postgres:16-alpine image: postgres:16-alpine
restart: unless-stopped restart: unless-stopped
environment: environment:
POSTGRES_USER: postgres POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: postgres POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
POSTGRES_DB: fiscal POSTGRES_DB: ${POSTGRES_DB:-fiscal}
volumes: volumes:
- postgres_data:/var/lib/postgresql/data - postgres_data:/var/lib/postgresql/data
healthcheck: healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d fiscal"] test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-fiscal}"]
interval: 5s interval: 5s
timeout: 5s timeout: 5s
retries: 10 retries: 10
@@ -21,19 +21,22 @@ services:
context: ./backend context: ./backend
dockerfile: Dockerfile dockerfile: Dockerfile
restart: unless-stopped restart: unless-stopped
env_file:
- path: ./.env
required: false
- path: ../.env
required: false
environment: environment:
DATABASE_URL: postgres://postgres:postgres@postgres:5432/fiscal DATABASE_URL: ${DATABASE_URL:-postgres://postgres:postgres@postgres:5432/fiscal}
PORT: 3001 PORT: ${PORT:-3001}
JWT_SECRET: change-this-to-a-random-secret-key POSTGRES_HOST: postgres
GITHUB_ID: ${GITHUB_ID} BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET:-local-dev-better-auth-secret-change-me}
GITHUB_SECRET: ${GITHUB_SECRET} BETTER_AUTH_BASE_URL: ${BETTER_AUTH_BASE_URL:-http://localhost:3001}
GOOGLE_ID: ${GOOGLE_ID}
GOOGLE_SECRET: ${GOOGLE_SECRET}
depends_on: depends_on:
postgres: postgres:
condition: service_healthy condition: service_healthy
healthcheck: healthcheck:
test: ["CMD", "bun", "-e", "require('http').createServer(() => {}).listen(3001)"] test: ["CMD-SHELL", "wget -q --spider http://localhost:3001/api/health || exit 1"]
interval: 30s interval: 30s
timeout: 10s timeout: 10s
retries: 3 retries: 3
@@ -44,9 +47,11 @@ services:
build: build:
context: ./frontend context: ./frontend
dockerfile: Dockerfile dockerfile: Dockerfile
args:
NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://backend:3001}
restart: unless-stopped restart: unless-stopped
environment: environment:
NEXT_PUBLIC_API_URL: http://backend:3001 NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://backend:3001}
depends_on: depends_on:
- backend - backend
networks: networks:

View File

@@ -9,6 +9,8 @@ RUN npm install
# Build # Build
FROM base AS builder FROM base AS builder
ARG NEXT_PUBLIC_API_URL=http://backend:3001
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps /app/node_modules ./node_modules
COPY . . COPY . .
RUN mkdir -p public && npm run build RUN mkdir -p public && npm run build
@@ -18,6 +20,8 @@ FROM base AS runner
WORKDIR /app WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
ARG NEXT_PUBLIC_API_URL=http://backend:3001
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
RUN addgroup --system --gid 1001 nodejs RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs RUN adduser --system --uid 1001 nextjs