From b847794e485776e37b11a6972fe5d08e7541adf2 Mon Sep 17 00:00:00 2001 From: Spaghet Date: Sun, 16 Nov 2025 19:39:39 -0500 Subject: [PATCH] Reducing Dockerfile complexity --- Dockerfile | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 32f3934..a219c96 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,31 +1,29 @@ -# Stage 1: Clone the repository and build the Astro project -FROM node:lts-alpine AS build +FROM node:22-alpine AS base WORKDIR /app -# Install git and clone the repository -RUN apk add --no-cache git -ARG REPO_URL -RUN git clone -b $REPO_BRANCH $REPO_URL . +# By copying only the package.json and package-lock.json here, we ensure that the following `-deps` steps are independent of the source code. +# Therefore, the `-deps` steps will be skipped if only the source code changes. +COPY package.json package-lock.json ./ -# Install dependencies and build the project +FROM base AS prod-deps +RUN npm install --omit=dev + +FROM base AS build-deps RUN npm install + +FROM build-deps AS build +COPY . . RUN npm run build -# Stage 2: Serve the built application with a lightweight runtime image -FROM node:lts-alpine AS runtime -WORKDIR /app +FROM base AS runtime +COPY --from=prod-deps /app/node_modules ./node_modules # Copy dependencies +COPY --from=build /app/dist ./dist # Copy the built output -# Copy only the necessary files from the build stage -COPY --from=build /app/dist ./dist -COPY --from=build /app/node_modules ./node_modules -COPY --from=build /app/package.json . - -# Set environment variables for the server +# Bind to all interfaces ENV HOST=0.0.0.0 +# Port to listen on ENV PORT=4321 - -# Expose the port the Astro app runs on +# Just convention, not required EXPOSE 4321 -# Command to run the application -CMD ["node", "./dist/server/entry.mjs"] \ No newline at end of file +CMD node ./dist/server/entry.mjs # Start the app \ No newline at end of file