Skip to content

Commit

Permalink
chore: fix dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoPorfirio01 committed Jan 2, 2025
1 parent ea5bc88 commit 737567d
Showing 1 changed file with 24 additions and 45 deletions.
69 changes: 24 additions & 45 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,47 @@ FROM node:20-alpine AS builder

# Define build argument
ARG DATABASE_URL

# Set environment variable
ENV DATABASE_URL=${DATABASE_URL}

# Instalar OpenSSL e outras dependências necessárias
RUN apk add --no-cache openssl openssl-dev

WORKDIR /app

# Instalar pnpm
RUN corepack enable && corepack prepare pnpm@latest --activate

# Copiar arquivos do workspace
COPY pnpm-workspace.yaml ./
COPY package.json ./
COPY pnpm-lock.yaml ./

# Copiar pacotes compartilhados
COPY packages/ ./packages/
COPY config/ ./config/
# Copy root package.json and workspace files
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml ./

# Copiar código da API
COPY apps/api/ ./apps/api/
COPY apps/api/prisma ./apps/api/prisma
# Copy API package.json
COPY apps/api/package.json ./apps/api/
COPY packages ./packages
COPY config ./config

# Instalar dependências
# Install dependencies
RUN pnpm install --frozen-lockfile

# Instalar dependências e garantir versões compatíveis do Prisma
RUN cd apps/api && \
pnpm add [email protected] && \
pnpm add @prisma/[email protected] && \
pnpm install
# Copy source code
COPY apps/api ./apps/api

# Gerar Prisma Client explicitamente
RUN cd apps/api && \
pnpm prisma generate --schema=./prisma/schema.prisma
# Build the application
WORKDIR /app/apps/api
RUN pnpm build

# Build após garantir que o Prisma Client foi gerado
RUN cd apps/api && \
pnpm build
# Generate Prisma Client in builder stage
RUN pnpm prisma generate --schema=./prisma/schema.prisma

# Imagem de produção
FROM node:20-alpine

# Define build argument and environment variable in production stage
ARG DATABASE_URL
ENV DATABASE_URL=${DATABASE_URL}
ENV NODE_ENV=production

# Create user earlier and set permissions properly
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Install OpenSSL in production image
RUN apk add --no-cache openssl

WORKDIR /app

RUN corepack enable && corepack prepare pnpm@latest --activate

# Copy files from builder
# Copy necessary files
COPY --from=builder /app/package.json ./
COPY --from=builder /app/pnpm-workspace.yaml ./
COPY --from=builder /app/pnpm-lock.yaml ./
Expand All @@ -73,25 +54,23 @@ COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/apps/api/node_modules ./apps/api/node_modules
COPY --from=builder /app/apps/api/prisma ./apps/api/prisma

# Important: Copy the generated Prisma Client
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma

# Install production dependencies
RUN pnpm install --prod --frozen-lockfile

# Set correct permissions for the app directory
# Set correct permissions
RUN chown -R appuser:appgroup /app

# Switch to appuser after setting permissions
USER appuser

WORKDIR /app/apps/api

EXPOSE 3333

# Generate Prisma client and run migrations
RUN pnpm prisma generate --schema=./prisma/schema.prisma && \
pnpm prisma migrate deploy
# Generate Prisma Client again in production stage
RUN pnpm prisma generate --schema=./prisma/schema.prisma

# HEALTHCHECK --interval=30s --timeout=3s \
# CMD wget --no-verbose --tries=1 --spider http://localhost:3333/health || exit 1
EXPOSE 3333

# Comando de inicialização
CMD ["pnpm", "start"]

0 comments on commit 737567d

Please sign in to comment.