Reducing Dockerfile complexity
This commit is contained in:
1 parent
017437d946
commit
b847794e48
1 file changed
+18
-20
+18
-20
@@ -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"]
|
||||
CMD node ./dist/server/entry.mjs # Start the app
|
||||
Reference in new issue
Block a user