diff --git a/Dockerfile b/Dockerfile index 0d05601..6e2f91c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,35 +1,38 @@ -FROM --platform=linux/arm64 node:18 AS pre +# 1. Base image with system dependencies +FROM --platform=linux/arm64 node:18 AS base WORKDIR /app + RUN apt-get update && \ - apt-get install -y python3 make g++ build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev next && \ + apt-get install -y python3 make g++ build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev && \ apt-get clean -# 1. Install dependencies only when needed -FROM --platform=linux/arm64 pre AS deps -WORKDIR /app -RUN npm install -g node-gyp -# COPY package.json yarn.lock* ./ -RUN yarn install +# Copy project files required for dependency installation +COPY package.json yarn.lock ./ -# 2. Rebuild the source code only when needed -FROM --platform=linux/arm64 pre AS builder +# 2. Install dependencies only when needed +FROM base AS deps +RUN yarn install --frozen-lockfile + +# 3. Rebuild the source code only when needed +FROM base AS builder WORKDIR /app + +# Copy all source code and installed node_modules COPY . . COPY --from=deps /app/node_modules ./node_modules -# Set dotenv using the build arguments +# Set dotenv using the build arguments (Next.js automatically picks up env vars prefixed with NEXT_PUBLIC_) ARG NEXT_PUBLIC_API_URL -RUN echo "NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL" >> .env.production ARG NEXT_PUBLIC_GA_ID -RUN echo "NEXT_PUBLIC_GA_ID=$NEXT_PUBLIC_GA_ID" >> .env.production -RUN yarn run build +# Next.js build step +RUN yarn build -# 3. Production image, copy all the files you need to run the app +# 4. Production image, copy only the necessary files FROM --platform=linux/arm64 node:18-alpine AS runner WORKDIR /app -# Install curl for health check +# Install curl for health checks RUN apk update && apk add --no-cache curl ENV NODE_ENV production @@ -41,8 +44,8 @@ COPY --from=builder /app/.next ./.next COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/package.json ./package.json -# Expose port +# Expose the application port EXPOSE 3000 # Start the Next.js app -CMD ["yarn", "start"] \ No newline at end of file +CMD ["yarn", "start"]