Refactor Dockerfile for improved efficiency

This commit is contained in:
dum 2026-02-27 11:20:08 +05:30 committed by GitHub
parent 0c65b8e9f7
commit cbc907cae4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,14 +1,18 @@
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
# Install curl (for Coolify healthchecks) and openssl (required by Prisma on Alpine)
RUN apk add --no-cache curl openssl
# 1. Copy dependency files first to maximize Docker layer caching
COPY package*.json ./
RUN npm install
# Install curl for healthchecks (required by Coolify)
RUN apk add --no-cache curl
# 2. Copy ONLY the prisma folder next to cache the Prisma Client generation
COPY prisma ./prisma/
# Define build arguments
ARG BUILD_DATABASE_URL="file:./dev.db"
ARG DATABASE_URL
ARG DATABASE_URL_DOCKER
ARG META_NAME
@ -21,7 +25,7 @@ ARG TRAKT_CLIENT_ID
ARG TRAKT_SECRET_ID
ARG NODE_ENV=production
ENV DATABASE_URL=${DATABASE_URL}
# Persist only non-sensitive runtime env vars (do NOT persist real DATABASE_URL)
ENV DATABASE_URL_DOCKER=${DATABASE_URL_DOCKER}
ENV META_NAME=${META_NAME}
ENV META_DESCRIPTION=${META_DESCRIPTION}
@ -33,12 +37,16 @@ ENV TRAKT_CLIENT_ID=${TRAKT_CLIENT_ID}
ENV TRAKT_SECRET_ID=${TRAKT_SECRET_ID}
ENV NODE_ENV=${NODE_ENV}
# 3. Generate Prisma client using the build-only placeholder URL
RUN DATABASE_URL=${BUILD_DATABASE_URL} npx prisma generate
# 4. Copy the rest of the application code AFTER dependencies and Prisma are sorted
COPY . .
RUN npx prisma generate
# Build the application
RUN npm run build
EXPOSE 3000
# Run migrations and start the server (kept exactly as you requested)
CMD ["sh", "-c", "npx prisma migrate deploy && node .output/server/index.mjs"]