From 05107b2b631fc4e69d86ee0b47a5153be55dc463 Mon Sep 17 00:00:00 2001 From: Rumazor Date: Sun, 8 Feb 2026 23:04:34 -0300 Subject: [PATCH] ejoras en el docker yaml --- backend/Dockerfile | 35 +++++++++++++++++++++++++++---- docker-compose.yml | 45 +++++++++++++++++++++++++++++----------- frontend/Dockerfile | 36 ++++++++++++++++++++++++++++---- frontend/next.config.mjs | 1 + 4 files changed, 97 insertions(+), 20 deletions(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 92ddeb4..e7cd323 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,15 +1,42 @@ -FROM node:latest +# Build stage +FROM node:20-alpine AS builder WORKDIR /app +# Copiar archivos de dependencias COPY package*.json ./ -RUN yarn install +# Instalar dependencias +RUN npm ci +# Copiar código fuente COPY . . -RUN yarn build +# Build +RUN npm run build + +# Production stage +FROM node:20-alpine + +WORKDIR /app + +# Crear usuario no-root +RUN addgroup -g 1001 -S nodejs && \ + adduser -S nestjs -u 1001 + +# Copiar package.json +COPY package*.json ./ + +# Instalar SOLO dependencias de producción +RUN npm ci --only=production && \ + npm cache clean --force + +# Copiar build desde stage anterior +COPY --from=builder /app/dist ./dist + +# Cambiar a usuario no-root +USER nestjs EXPOSE 3000 -CMD ["yarn", "start:prod"] \ No newline at end of file +CMD ["node", "dist/main"] diff --git a/docker-compose.yml b/docker-compose.yml index 491d545..002e4b9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ services: build: context: ./backend dockerfile: Dockerfile + restart: unless-stopped ports: - "3001:3000" environment: @@ -25,26 +26,42 @@ services: condition: service_healthy networks: - my_network + healthcheck: + test: ["CMD", "node", "-e", "require('http').get('http://localhost:3000/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"] + interval: 30s + timeout: 3s + retries: 3 + start_period: 40s nextjs: build: context: ./frontend dockerfile: Dockerfile container_name: nextjs_app + restart: unless-stopped ports: - "3000:3000" environment: BASE_API_URL: "http://nestjs:3000" NEXT_PUBLIC_API_URL: "http://localhost:3001" depends_on: - - nestjs + nestjs: + condition: service_healthy networks: - my_network + healthcheck: + test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/api/health"] + interval: 30s + timeout: 3s + retries: 3 + start_period: 40s postgres: - image: postgres:14.3 - ports: - - "5432:5432" + image: postgres:14-alpine # 🔥 50% más pequeña + restart: unless-stopped + # Solo exponer si necesitas acceso directo desde tu máquina + # ports: + # - "5432:5432" environment: POSTGRES_USER: "${DB_USER}" POSTGRES_PASSWORD: "${DB_PASSWORD}" @@ -52,25 +69,29 @@ services: volumes: - postgres_data:/var/lib/postgresql/data healthcheck: - test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"] + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER"] interval: 10s - timeout: 5s - retries: 5 + timeout: 3s + retries: 3 + start_period: 10s networks: - my_network redis: image: redis:7-alpine - ports: - - "6379:6379" - command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru + restart: unless-stopped + # Solo exponer si necesitas acceso directo desde tu máquina + # ports: + # - "6379:6379" + command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru --save "" volumes: - redis_data:/data healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 10s - timeout: 5s - retries: 5 + timeout: 3s + retries: 3 + start_period: 5s networks: - my_network diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 0df9462..ee16b1a 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,15 +1,43 @@ -FROM node:latest +# Dependencies stage +FROM node:20-alpine AS deps WORKDIR /app -COPY package*.json ./ +COPY package.json yarn.lock ./ +RUN yarn install --frozen-lockfile -RUN yarn install +# Builder stage +FROM node:20-alpine AS builder +WORKDIR /app + +COPY --from=deps /app/node_modules ./node_modules COPY . . +ENV NEXT_TELEMETRY_DISABLED=1 + RUN yarn build +# Production stage +FROM node:20-alpine AS runner + +WORKDIR /app + +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN addgroup -g 1001 -S nodejs && \ + adduser -S nextjs -u 1001 + +COPY --from=builder /app/public ./public +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + EXPOSE 3000 -CMD ["yarn", "start"] +ENV PORT=3000 +ENV HOSTNAME="0.0.0.0" + +CMD ["node", "server.js"] diff --git a/frontend/next.config.mjs b/frontend/next.config.mjs index e85a058..108320b 100644 --- a/frontend/next.config.mjs +++ b/frontend/next.config.mjs @@ -56,6 +56,7 @@ const withPWA = withPWAInit({ /** @type {import('next').NextConfig} */ const nextConfig = { + output: "standalone", typescript: { ignoreBuildErrors: true, },