Skip to content

Commit ed6d162

Browse files
committed
refactor: simplify Dockerfile.api by removing multi-stage build and unnecessary dependencies
1 parent c35d0da commit ed6d162

File tree

1 file changed

+19
-79
lines changed

1 file changed

+19
-79
lines changed

Docker/Dockerfile.api

Lines changed: 19 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,35 @@
1-
# Use an ARG to allow for easy version upgrades
2-
ARG NODE_VERSION=22
1+
FROM node:22-slim
32

4-
# =================================================================================================
5-
# 1. Builder Stage: Installs all dependencies and builds the source code.
6-
# =================================================================================================
7-
FROM node:${NODE_VERSION}-slim AS builder
8-
9-
# Set environment variables for pnpm
10-
ENV PNPM_HOME="/pnpm"
11-
ENV PATH="$PNPM_HOME:$PATH"
12-
RUN corepack enable && corepack prepare pnpm@latest --activate
13-
14-
WORKDIR /usr/src/app
15-
16-
# Install build tools needed for native modules (like Prisma)
17-
RUN apt-get update && apt-get install -y --no-install-recommends \
3+
# 🛠 Install OpenSSL 1.1 (needed by Prisma) and other required packages
4+
RUN apt-get update && apt-get install -y \
185
openssl \
196
libssl-dev \
20-
pkg-config \
21-
build-essential \
22-
python3 \
7+
libstdc++6 \
8+
zlib1g \
9+
bash \
2310
&& rm -rf /var/lib/apt/lists/*
2411

25-
# Copy files required to install dependencies to maximize layer caching
26-
COPY pnpm-lock.yaml pnpm-workspace.yaml ./
27-
COPY package.json ./
28-
COPY packages/ packages/
29-
COPY apps/api/package.json apps/api/
30-
31-
# Use `pnpm fetch` to download all dependencies into a shared store.
32-
RUN pnpm fetch
33-
34-
# Install all dependencies (including devDependencies) using the fetched packages
35-
RUN pnpm install --frozen-lockfile --offline
36-
37-
# Copy the rest of the source code
38-
COPY . .
39-
40-
# Generate Prisma client
41-
RUN pnpm db:generate
42-
43-
# Build the specific 'api' application
44-
RUN pnpm build --filter=api
45-
46-
# =================================================================================================
47-
# 2. Production Stage: Creates the final, small, and secure image.
48-
# =================================================================================================
49-
FROM node:${NODE_VERSION}-slim AS production
50-
51-
ENV NODE_ENV=production
52-
ENV PNPM_HOME="/pnpm"
53-
ENV PATH="$PNPM_HOME:$PATH"
54-
RUN corepack enable && corepack prepare pnpm@latest --activate
12+
RUN npm install -g pnpm@9.9.0
5513

5614
WORKDIR /usr/src/app
5715

58-
# Install only the runtime dependency for Prisma (OpenSSL)
59-
RUN apt-get update && apt-get install -y --no-install-recommends \
60-
openssl \
61-
&& rm -rf /var/lib/apt/lists/*
62-
63-
# Create a non-root user for security
64-
RUN addgroup --system --gid 1001 nodejs
65-
RUN adduser --system --uid 1001 nodejs
16+
COPY ./packages ./packages
17+
COPY ./pnpm-lock.yaml ./pnpm-lock.yaml
18+
COPY ./pnpm-workspace.yaml ./pnpm-workspace.yaml
6619

67-
# === THE KEY CHANGE IS HERE ===
68-
# Instead of copying the pruned node_modules, we will reinstall only production dependencies.
20+
COPY ./package.json ./package.json
21+
COPY ./tsconfig.json ./tsconfig.json
22+
COPY ./turbo.json ./turbo.json
6923

70-
# First, copy over the necessary package manifests and lockfile from the builder
71-
COPY --from=builder /usr/src/app/pnpm-lock.yaml ./
72-
COPY --from=builder /usr/src/app/pnpm-workspace.yaml ./
73-
COPY --from=builder /usr/src/app/package.json ./
74-
COPY --from=builder /usr/src/app/packages/ packages/
75-
COPY --from=builder /usr/src/app/apps/api/package.json apps/api/
24+
COPY ./apps/api ./apps/api
7625

77-
# Now, install ONLY production dependencies using the packages already fetched by the builder stage.
78-
# This creates a clean, production-only node_modules folder.
79-
RUN pnpm install --prod --frozen-lockfile --offline
26+
RUN pnpm install --frozen-lockfile
8027

81-
# Copy the built application code and Prisma schema from the builder stage
82-
COPY --from=builder --chown=nodejs:nodejs /usr/src/app/apps/api/dist ./apps/api/dist
83-
COPY --from=builder --chown=nodejs:nodejs /usr/src/app/packages/db/prisma ./apps/api/prisma
28+
# Generate Prisma client
29+
RUN pnpm db:generate
8430

85-
# Switch to the non-root user
86-
USER nodejs
31+
RUN pnpm build --filter=api
8732

88-
# Change to the app-specific directory
8933
WORKDIR /usr/src/app/apps/api
9034

91-
# Expose the port your app runs on
92-
EXPOSE 5000
93-
94-
# Start the application
95-
CMD ["node", "dist/index.js"]
35+
CMD ["pnpm", "start"]

0 commit comments

Comments
 (0)