Reducing Dockerfile complexity

This commit is contained in:
spaghet committed 2025-11-16 19:39:39 -05:00
1 parent 017437d946
commit b847794e48
1 file changed
+18 -20
+18 -20
View File
@@ -1,31 +1,29 @@
# Stage 1: Clone the repository and build the Astro project FROM node:22-alpine AS base
FROM node:lts-alpine AS build
WORKDIR /app WORKDIR /app
# Install git and clone the repository # By copying only the package.json and package-lock.json here, we ensure that the following `-deps` steps are independent of the source code.
RUN apk add --no-cache git # Therefore, the `-deps` steps will be skipped if only the source code changes.
ARG REPO_URL COPY package.json package-lock.json ./
RUN git clone -b $REPO_BRANCH $REPO_URL .
# Install dependencies and build the project FROM base AS prod-deps
RUN npm install --omit=dev
FROM base AS build-deps
RUN npm install RUN npm install
FROM build-deps AS build
COPY . .
RUN npm run build RUN npm run build
# Stage 2: Serve the built application with a lightweight runtime image FROM base AS runtime
FROM node:lts-alpine AS runtime COPY --from=prod-deps /app/node_modules ./node_modules # Copy dependencies
WORKDIR /app COPY --from=build /app/dist ./dist # Copy the built output
# Copy only the necessary files from the build stage # Bind to all interfaces
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
ENV HOST=0.0.0.0 ENV HOST=0.0.0.0
# Port to listen on
ENV PORT=4321 ENV PORT=4321
# Just convention, not required
# Expose the port the Astro app runs on
EXPOSE 4321 EXPOSE 4321
# Command to run the application CMD node ./dist/server/entry.mjs # Start the app
CMD ["node", "./dist/server/entry.mjs"]