-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95e25ca
commit 232a7f3
Showing
1 changed file
with
51 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,63 @@ | ||
# Use the official Node.js image with Alpine Linux | ||
FROM node:20-alpine | ||
# 1. Base Image | ||
FROM node:20-alpine AS base | ||
|
||
# Set the working directory in the container | ||
# Install dependencies like libc6-compat if required | ||
RUN apk add --no-cache libc6-compat | ||
|
||
# Set the working directory | ||
WORKDIR /app | ||
|
||
# Copy package.json and yarn.lock to the working directory | ||
# Enable Corepack and configure Yarn version | ||
RUN corepack enable | ||
RUN yarn set version 4.1.0 | ||
|
||
# 2. Dependencies Layer | ||
FROM base AS deps | ||
|
||
# Copy dependency management files | ||
COPY package.json yarn.lock ./ | ||
|
||
# Install dependencies using Yarn | ||
RUN yarn install --frozen-lockfile | ||
# Install dependencies | ||
RUN \ | ||
if [ -f yarn.lock ]; then yarn install --frozen-lockfile; \ | ||
elif [ -f package-lock.json ]; then npm ci; \ | ||
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i; \ | ||
else echo "Lockfile not found." && exit 1; \ | ||
fi | ||
|
||
# Copy the rest of the application code | ||
# 3. Build Layer | ||
FROM base AS builder | ||
|
||
# Copy app source and installed dependencies | ||
COPY --from=deps /app/node_modules ./node_modules | ||
COPY . . | ||
|
||
# Build the application for production | ||
# Set the environment to production | ||
ENV NODE_ENV=production | ||
|
||
# Build the app | ||
RUN yarn build | ||
|
||
# Expose the port the app runs on (optional) | ||
# 4. Runner (Production Image) | ||
FROM base AS runner | ||
|
||
# Set environment variables | ||
ENV NODE_ENV=production | ||
ENV PORT=3000 | ||
|
||
# Add a non-root user for better security | ||
RUN addgroup -g 1001 -S nodejs | ||
RUN adduser -S appuser -u 1001 | ||
USER appuser | ||
|
||
# Copy built files and dependencies | ||
COPY --from=builder /app/build ./build | ||
COPY --from=builder /app/node_modules ./node_modules | ||
COPY --from=builder /app/package.json ./package.json | ||
COPY --from=builder /app/public ./public | ||
|
||
# Expose the port the app will run on | ||
EXPOSE 3000 | ||
|
||
# Define the command to run the app | ||
CMD ["yarn", "start"] | ||
# Command to run the app | ||
CMD ["node", "build/server.js"] |