feat: enhance Docker setup and deployment configuration
This commit is contained in:
29
.dockerignore
Normal file
29
.dockerignore
Normal file
@@ -0,0 +1,29 @@
|
||||
# Dependencias y artefactos locales: el build los regenera adentro de la imagen.
|
||||
node_modules
|
||||
**/node_modules
|
||||
|
||||
# Se generan en el builder (prisma generate, vite build).
|
||||
apps/backend/generated
|
||||
apps/backend/dist
|
||||
apps/web/dist
|
||||
.turbo
|
||||
**/.turbo
|
||||
|
||||
# Secretos: en Dokploy llegan por variables de entorno.
|
||||
.env
|
||||
**/.env
|
||||
!.env.example
|
||||
**/.env.example
|
||||
|
||||
# Ruido del repo.
|
||||
.git
|
||||
.gitignore
|
||||
.github
|
||||
.DS_Store
|
||||
*.log
|
||||
README.md
|
||||
stack.md
|
||||
skills-lock.json
|
||||
**/skills-lock.json
|
||||
.agents
|
||||
**/.agents
|
||||
66
Dockerfile.backend
Normal file
66
Dockerfile.backend
Normal file
@@ -0,0 +1,66 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
# ─── Base ────────────────────────────────────────────────────────────────────
|
||||
FROM oven/bun:1.4-alpine AS base
|
||||
WORKDIR /app
|
||||
ENV CI=1
|
||||
|
||||
# ─── Dependencias ────────────────────────────────────────────────────────────
|
||||
# Solo los package.json:bun no necesita el código fuente para resolver el lockfile.
|
||||
FROM base AS deps
|
||||
COPY package.json bun.lock bunfig.toml turbo.json ./
|
||||
COPY apps/backend/package.json ./apps/backend/
|
||||
COPY apps/web/package.json ./apps/web/
|
||||
COPY packages/config/package.json ./packages/config/
|
||||
COPY packages/shared/package.json ./packages/shared/
|
||||
# El cache global de bun pesa ~370MB: se borra en la misma capa.
|
||||
RUN bun install --frozen-lockfile && rm -rf /root/.bun/install
|
||||
|
||||
# ─── Prisma Client ───────────────────────────────────────────────────────────
|
||||
# El generator "prisma-client" emite TypeScript, así que el cliente generado se
|
||||
# ejecuta con Bun sin compilar nada. La URL es un placeholder: `generate` no
|
||||
# necesita conexión, solo leer el schema.
|
||||
FROM deps AS generate
|
||||
ARG DATABASE_URL=postgresql://user:password@localhost:5432/placeholder
|
||||
ENV DATABASE_URL=$DATABASE_URL
|
||||
COPY apps/backend/prisma.config.ts ./apps/backend/
|
||||
COPY apps/backend/prisma ./apps/backend/prisma
|
||||
WORKDIR /app/apps/backend
|
||||
RUN bun run db:generate
|
||||
|
||||
# ─── Imagen de la API ────────────────────────────────────────────────────────
|
||||
FROM base AS api
|
||||
ENV NODE_ENV=production \
|
||||
PORT=4000
|
||||
|
||||
COPY package.json bun.lock bunfig.toml ./
|
||||
COPY apps/backend/package.json ./apps/backend/
|
||||
COPY packages/config/package.json ./packages/config/
|
||||
COPY packages/shared/package.json ./packages/shared/
|
||||
# `--production` alcanza para la API: el CLI de Prisma y typescript son
|
||||
# devDependencies y no hacen falta para servir tráfico.
|
||||
RUN bun install --frozen-lockfile --production && rm -rf /root/.bun/install
|
||||
|
||||
COPY --from=generate /app/apps/backend/generated ./apps/backend/generated
|
||||
COPY apps/backend/tsconfig.json ./apps/backend/
|
||||
COPY apps/backend/src ./apps/backend/src
|
||||
# @gruperly/shared se consume como fuente TS; @gruperly/config aporta el
|
||||
# tsconfig base del que extiende el tsconfig del backend.
|
||||
COPY packages ./packages
|
||||
|
||||
WORKDIR /app/apps/backend
|
||||
EXPOSE 4000
|
||||
# Sin `${...}`: el CMD corre bajo /bin/sh (ash), donde eso es un error de sintaxis.
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
|
||||
CMD bun --eval "fetch('http://127.0.0.1:4000/api/v1/health').then((r) => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
||||
|
||||
# Bun ejecuta el TypeScript directamente y resuelve `@/*` con el tsconfig.json,
|
||||
# igual que `bun start` en local.
|
||||
CMD ["bun", "src/server.ts"]
|
||||
|
||||
# ─── Imagen de migraciones ───────────────────────────────────────────────────
|
||||
# Stage aparte: el CLI de Prisma es devDependency y no viaja en la imagen final.
|
||||
# Corre una sola vez por deploy y termina.
|
||||
FROM generate AS migrate
|
||||
WORKDIR /app/apps/backend
|
||||
CMD ["bun", "run", "db:deploy"]
|
||||
35
Dockerfile.web
Normal file
35
Dockerfile.web
Normal file
@@ -0,0 +1,35 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
# ─── Base ────────────────────────────────────────────────────────────────────
|
||||
FROM oven/bun:1.4-alpine AS base
|
||||
WORKDIR /app
|
||||
ENV CI=1
|
||||
|
||||
# ─── Dependencias ────────────────────────────────────────────────────────────
|
||||
FROM base AS deps
|
||||
COPY package.json bun.lock bunfig.toml turbo.json ./
|
||||
COPY apps/backend/package.json ./apps/backend/
|
||||
COPY apps/web/package.json ./apps/web/
|
||||
COPY packages/config/package.json ./packages/config/
|
||||
COPY packages/shared/package.json ./packages/shared/
|
||||
# El cache global de bun pesa ~370MB: se borra en la misma capa.
|
||||
RUN bun install --frozen-lockfile && rm -rf /root/.bun/install
|
||||
|
||||
# ─── Build de Vite ───────────────────────────────────────────────────────────
|
||||
FROM deps AS build
|
||||
# Vite reemplaza import.meta.env.* en tiempo de build, no de runtime.
|
||||
# Vacío = mismo origen: el proxy /api de nginx hace de API.
|
||||
ARG VITE_API_URL=""
|
||||
ENV VITE_API_URL=$VITE_API_URL
|
||||
COPY apps/web ./apps/web
|
||||
COPY packages ./packages
|
||||
RUN bun --filter @gruperly/web build
|
||||
|
||||
# ─── Imagen final ────────────────────────────────────────────────────────────
|
||||
FROM nginx:1.27-alpine AS web
|
||||
COPY apps/web/nginx.conf /etc/nginx/conf.d/default.conf
|
||||
COPY --from=build /app/apps/web/dist /usr/share/nginx/html
|
||||
EXPOSE 80
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
||||
CMD wget --quiet --tries=1 --spider http://127.0.0.1/ || exit 1
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
31
README.md
31
README.md
@@ -37,3 +37,34 @@ bun --filter @gruperly/backend db:studio # Abre Prisma Studio
|
||||
|
||||
1. Crea una base PostgreSQL y copia `apps/backend/.env.example` → `apps/backend/.env`, ajustando `DATABASE_URL`.
|
||||
2. Ejecuta `bun --filter @gruperly/backend db:generate` y `bun --filter @gruperly/backend db:push` (o `bun --filter @gruperly/backend db:migrate` en desarrollo).
|
||||
|
||||
## Deploy en producción (Docker / Dokploy)
|
||||
|
||||
Hay dos imágenes y un compose: `Dockerfile.backend` (target `api`), `Dockerfile.backend` (target `migrate`) y `Dockerfile.web`.
|
||||
|
||||
- `migrate` corre `prisma migrate deploy` y sale. `api` no arranca hasta que termina bien (`service_completed_successfully`).
|
||||
- `api` corre Bun sobre el TypeScript fuente (`bun src/server.ts`), igual que en local: el cliente de Prisma se genera como TS y el tsconfig resuelve los aliases `@/*`.
|
||||
- `web` compila con Vite y lo sirve nginx, que además hace de proxy de `/api` hacia `api`. Así el navegador siempre pega al mismo origen: la cookie de sesión es first-party y el CORS del backend no se activa.
|
||||
|
||||
### Variables de entorno
|
||||
|
||||
Se leen del entorno de Dokploy (o de un `.env` en la raíz, que está gitignoreado):
|
||||
|
||||
| Variable | Required | Descripción |
|
||||
| --- | --- | --- |
|
||||
| `DATABASE_URL` | sí | `postgresql://user:pass@host:5432/gruperly?schema=public` |
|
||||
| `PUBLIC_URL` | sí | URL pública de la app. Se usa como `WEB_URL` y `BETTER_AUTH_URL` (mismo origen por el proxy de nginx) |
|
||||
| `BETTER_AUTH_SECRET` | sí | `openssl rand -base64 32` |
|
||||
| `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` | no | Login social. Si faltan, no se registra el provider |
|
||||
| `EMAIL_PROVIDER`, `EMAIL_SERVER_*`, `EMAIL_FROM` | no | Salida de emails (ver `apps/backend/.env.example`) |
|
||||
| `VITE_API_URL` | no | Se hornea en el bundle de web. Vacío = mismo origen. Solo si querés la API en otro dominio |
|
||||
|
||||
En Dokploy: creá una aplicación tipo **Compose**, apuntando al repo, y asociá el dominio solo al servicio `web` (puerto `8080`, que publica `80`). Las migraciones corren solas en cada deploy.
|
||||
|
||||
### Comprobar el compose en local
|
||||
|
||||
```bash
|
||||
docker compose up --build -d
|
||||
docker compose logs -f migrate api # migrate debe salir con "No pending migrations to apply"
|
||||
curl http://localhost:8080/api/v1/health
|
||||
```
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
"lint:fix": "biome check . --write",
|
||||
"db:generate": "prisma generate",
|
||||
"db:migrate": "prisma migrate dev",
|
||||
"db:deploy": "prisma migrate deploy",
|
||||
"db:push": "prisma db push",
|
||||
"db:studio": "prisma studio"
|
||||
},
|
||||
@@ -24,6 +25,7 @@
|
||||
"@prisma/client": "^7.10.0",
|
||||
"better-auth": "1.7.2",
|
||||
"hono": "^4.13.3",
|
||||
"nanoid": "^3.3.19",
|
||||
"nodemailer": "^9.0.6",
|
||||
"pino": "^9.5.0",
|
||||
"pino-pretty": "^13.0.0",
|
||||
|
||||
52
apps/web/nginx.conf
Normal file
52
apps/web/nginx.conf
Normal file
@@ -0,0 +1,52 @@
|
||||
# Nginx para el build de @gruperly/web.
|
||||
# Sirve los assets estáticos y hace de proxy de /api hacia el servicio `api`.
|
||||
# Con el proxy, el navegador pega siempre al mismo origen: la cookie de sesión
|
||||
# es first-party y el CORS del backend queda como red de seguridad.
|
||||
|
||||
# DNS interno de Docker: resuelve `api` en cada request, así el proxy sobrevive
|
||||
# a un recreate del contenedor (que cambia la IP).
|
||||
resolver 127.0.0.11 valid=10s ipv6=off;
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
client_max_body_size 10m;
|
||||
|
||||
# ── API ────────────────────────────────────────────────────────────────────
|
||||
location /api/ {
|
||||
set $api http://api:4000;
|
||||
proxy_pass $api$request_uri;
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
proxy_set_header Connection "";
|
||||
|
||||
proxy_connect_timeout 10s;
|
||||
proxy_read_timeout 60s;
|
||||
}
|
||||
|
||||
# ── Assets con hash en el nombre ───────────────────────────────────────────
|
||||
# Solo `add_header`: `expires` agregaría un segundo Cache-Control.
|
||||
location /assets/ {
|
||||
add_header Cache-Control "public, max-age=31536000, immutable";
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
# ── SPA ────────────────────────────────────────────────────────────────────
|
||||
location = /index.html {
|
||||
add_header Cache-Control "no-cache";
|
||||
}
|
||||
|
||||
location / {
|
||||
add_header Cache-Control "no-cache";
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
}
|
||||
1
bun.lock
1
bun.lock
@@ -19,6 +19,7 @@
|
||||
"@prisma/client": "^7.10.0",
|
||||
"better-auth": "1.7.2",
|
||||
"hono": "^4.13.3",
|
||||
"nanoid": "^3.3.19",
|
||||
"nodemailer": "^9.0.6",
|
||||
"pino": "^9.5.0",
|
||||
"pino-pretty": "^13.0.0",
|
||||
|
||||
62
docker-compose.yml
Normal file
62
docker-compose.yml
Normal file
@@ -0,0 +1,62 @@
|
||||
# Deploy en Dokploy (o `docker compose up --build` en cualquier lado).
|
||||
#
|
||||
# El dominio público se expone una sola vez, en `web`: nginx sirve el bundle de
|
||||
# Vite y hace de proxy de /api hacia el contenedor `api`. Eso deja la cookie de
|
||||
# sesión first-party (no hace falta SameSite=None) y vuelve innecesario el
|
||||
# preflight CORS. Si preferís subdominio aparte para la API, quitá el bloque
|
||||
# `location /api/` de apps/web/nginx.conf y seteá VITE_API_URL al construir web.
|
||||
|
||||
x-backend-env: &backend-env
|
||||
NODE_ENV: production
|
||||
# Web y API comparten dominio público: mismo origen para cookies y CORS.
|
||||
DATABASE_URL: ${DATABASE_URL:?definí DATABASE_URL (postgresql://user:pass@host:5432/gruperly?schema=public)}
|
||||
BETTER_AUTH_URL: ${PUBLIC_URL:?definí PUBLIC_URL con la URL pública de la app}
|
||||
BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET:?definí BETTER_AUTH_SECRET (openssl rand -base64 32)}
|
||||
WEB_URL: ${PUBLIC_URL:?definí PUBLIC_URL con la URL pública de la app}
|
||||
GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID:-}
|
||||
GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET:-}
|
||||
EMAIL_PROVIDER: ${EMAIL_PROVIDER:-smtp}
|
||||
EMAIL_SERVER_HOST: ${EMAIL_SERVER_HOST:-}
|
||||
EMAIL_SERVER_PORT: ${EMAIL_SERVER_PORT:-587}
|
||||
EMAIL_SERVER_USER: ${EMAIL_SERVER_USER:-}
|
||||
EMAIL_SERVER_PASSWORD: ${EMAIL_SERVER_PASSWORD:-}
|
||||
EMAIL_FROM: ${EMAIL_FROM:-Gruperly <no-reply@gruperly.com>}
|
||||
|
||||
services:
|
||||
# Corre una vez por deploy: aplica prisma/migrations y sale con código 0.
|
||||
# No reinicia nunca; si falla, `api` no arranca (ver depends_on de más abajo).
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.backend
|
||||
target: migrate
|
||||
environment:
|
||||
DATABASE_URL: ${DATABASE_URL:?definí DATABASE_URL (postgresql://user:pass@host:5432/gruperly?schema=public)}
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.backend
|
||||
target: api
|
||||
environment: *backend-env
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
expose:
|
||||
- "4000"
|
||||
|
||||
web:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.web
|
||||
target: web
|
||||
args:
|
||||
# Se hornea en el bundle. Vacío = mismo origen (proxy /api de nginx).
|
||||
VITE_API_URL: ${VITE_API_URL:-}
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
- api
|
||||
ports:
|
||||
- "8080:80"
|
||||
Reference in New Issue
Block a user