refactor: optimize Dockerfile by restructuring build steps and improving clarity

This commit is contained in:
2026-04-20 07:54:46 +02:00
parent 287dfc0b8b
commit 3c4e10eda7

View File

@@ -1,34 +1,50 @@
# Build stage
FROM node:24-trixie-slim AS build FROM node:24-trixie-slim AS build
WORKDIR /app WORKDIR /app
COPY backend/package*.json backend/ # Copy sources required for build
COPY frontend/package*.json frontend/ COPY backend/*.json backend/
RUN cd backend && npm ci COPY backend/src backend/src
RUN cd frontend && npm ci
COPY . . COPY frontend/*.json frontend/
COPY frontend/vite.config.ts frontend/vite.config.ts
COPY frontend/index.html frontend/index.html
COPY frontend/src frontend/src
COPY frontend/test frontend/test
RUN cd backend && npm run build COPY templates templates
RUN cd frontend && npm run build COPY templates.json templates.json
RUN cd backend && npm prune --omit=dev
# Build backend and frontend
RUN cd backend && npm ci && npm run build && npm prune --omit=dev
RUN cd frontend && npm ci && npm run build
# Build the container
FROM node:24-trixie-slim AS runtime FROM node:24-trixie-slim AS runtime
WORKDIR /app WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
# Copy built artifacts
COPY --from=build /app/dist dist COPY --from=build /app/dist dist
COPY --from=build /app/templates templates COPY --from=build /app/templates templates
COPY --from=build /app/templates.json templates.json COPY --from=build /app/templates.json templates.json
COPY --from=build /app/backend/node_modules dist/backend/node_modules COPY --from=build /app/backend/node_modules dist/backend/node_modules
WORKDIR /app # Copy entrypoint and healthcheck scripts
COPY entrypoint.sh entrypoint.sh COPY entrypoint.sh entrypoint.sh
COPY healthcheck.js healthcheck.js COPY healthcheck.js healthcheck.js
# Ensure entrypoint script is executable
RUN chmod +x entrypoint.sh RUN chmod +x entrypoint.sh
# Set environment variables and expose port
ENV NODE_ENV=production
ENV PORT=3000
EXPOSE 3000 EXPOSE 3000
# Configure health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD ["node", "/app/healthcheck.js"] HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD ["node", "/app/healthcheck.js"]
# Configure entrypoint
ENTRYPOINT ["./entrypoint.sh"] ENTRYPOINT ["./entrypoint.sh"]