mirror of
https://github.com/TheNetsky/Microsoft-Rewards-Script.git
synced 2026-01-21 15:33:58 +00:00
Dockerfile and image optimization (#369)
Refactor of dockerfile to reduce final image from 2.55 gb to 766 mb to support pre-build
This commit is contained in:
committed by
GitHub
parent
15f62963f8
commit
554227e200
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,7 +2,6 @@ sessions/
|
|||||||
dist/
|
dist/
|
||||||
node_modules/
|
node_modules/
|
||||||
.github/
|
.github/
|
||||||
package-lock.json
|
|
||||||
accounts.json
|
accounts.json
|
||||||
notes
|
notes
|
||||||
accounts.dev.json
|
accounts.dev.json
|
||||||
|
|||||||
99
Dockerfile
99
Dockerfile
@@ -1,63 +1,80 @@
|
|||||||
###############################################################################
|
###############################################################################
|
||||||
# Stage 1: Builder (compile TypeScript)
|
# Stage 1: Builder
|
||||||
###############################################################################
|
###############################################################################
|
||||||
FROM node:22-slim AS builder
|
FROM node:22-slim AS builder
|
||||||
|
|
||||||
WORKDIR /usr/src/microsoft-rewards-script
|
WORKDIR /usr/src/microsoft-rewards-script
|
||||||
|
|
||||||
# Install minimal tooling if needed
|
ENV PLAYWRIGHT_BROWSERS_PATH=0
|
||||||
RUN apt-get update \
|
|
||||||
&& apt-get install -y --no-install-recommends ca-certificates \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Copy package manifests
|
# Copy package files
|
||||||
COPY package*.json tsconfig.json ./
|
COPY package.json package-lock.json tsconfig.json ./
|
||||||
|
|
||||||
# Conditional install: npm ci if lockfile exists, else npm install
|
# Install all dependencies required to build the script
|
||||||
RUN if [ -f package-lock.json ]; then \
|
RUN npm ci --ignore-scripts
|
||||||
npm ci --ignore-scripts; \
|
|
||||||
else \
|
|
||||||
npm install --ignore-scripts; \
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Copy source code
|
# Copy source and build
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Build TypeScript
|
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
|
# Remove build dependencies, and reinstall only runtime dependencies
|
||||||
|
RUN rm -rf node_modules \
|
||||||
|
&& npm ci --omit=dev --ignore-scripts \
|
||||||
|
&& npm cache clean --force
|
||||||
|
|
||||||
|
# Install Chromium Headless Shell, and cleanup
|
||||||
|
RUN npx playwright install --with-deps --only-shell chromium \
|
||||||
|
&& rm -rf /root/.cache /tmp/* /var/tmp/*
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Stage 2: Runtime (Playwright image)
|
# Stage 2: Runtime
|
||||||
###############################################################################
|
###############################################################################
|
||||||
FROM node:22-slim AS runtime
|
FROM node:22-slim AS runtime
|
||||||
|
|
||||||
WORKDIR /usr/src/microsoft-rewards-script
|
WORKDIR /usr/src/microsoft-rewards-script
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
# Set production environment variables
|
||||||
ENV TZ=UTC
|
ENV NODE_ENV=production \
|
||||||
# Use shared location for Playwright browsers so both 'playwright' and 'rebrowser-playwright' can find them
|
TZ=UTC \
|
||||||
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
|
PLAYWRIGHT_BROWSERS_PATH=0 \
|
||||||
# Force headless in container to be compatible with Chromium Headless Shell
|
FORCE_HEADLESS=1
|
||||||
ENV FORCE_HEADLESS=1
|
|
||||||
|
|
||||||
# Copy package files first for better caching
|
# Install minimal system libraries required for Chromium headless to run
|
||||||
COPY --from=builder /usr/src/microsoft-rewards-script/package*.json ./
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
ca-certificates \
|
||||||
|
libglib2.0-0 \
|
||||||
|
libdbus-1-3 \
|
||||||
|
libexpat1 \
|
||||||
|
libfontconfig1 \
|
||||||
|
libgtk-3-0 \
|
||||||
|
libnspr4 \
|
||||||
|
libnss3 \
|
||||||
|
libasound2 \
|
||||||
|
libflac12 \
|
||||||
|
libatk1.0-0 \
|
||||||
|
libatspi2.0-0 \
|
||||||
|
libdrm2 \
|
||||||
|
libgbm1 \
|
||||||
|
libdav1d6 \
|
||||||
|
libx11-6 \
|
||||||
|
libx11-xcb1 \
|
||||||
|
libxcomposite1 \
|
||||||
|
libxcursor1 \
|
||||||
|
libxdamage1 \
|
||||||
|
libxext6 \
|
||||||
|
libxfixes3 \
|
||||||
|
libxi6 \
|
||||||
|
libxrandr2 \
|
||||||
|
libxrender1 \
|
||||||
|
libxss1 \
|
||||||
|
libxtst6 \
|
||||||
|
libdouble-conversion3 \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
|
||||||
|
|
||||||
# Install only production dependencies, with fallback
|
# Copy compiled application and dependencies from builder stage
|
||||||
RUN if [ -f package-lock.json ]; then \
|
|
||||||
npm ci --omit=dev --ignore-scripts; \
|
|
||||||
else \
|
|
||||||
npm install --production --ignore-scripts; \
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Install only Chromium Headless Shell and its OS deps (smaller than full browser set)
|
|
||||||
# This will install required apt packages internally; we clean up afterwards to keep the image slim.
|
|
||||||
RUN npx playwright install --with-deps --only-shell \
|
|
||||||
&& apt-get clean \
|
|
||||||
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/*.bin || true
|
|
||||||
|
|
||||||
# Copy built application
|
|
||||||
COPY --from=builder /usr/src/microsoft-rewards-script/dist ./dist
|
COPY --from=builder /usr/src/microsoft-rewards-script/dist ./dist
|
||||||
|
COPY --from=builder /usr/src/microsoft-rewards-script/package*.json ./
|
||||||
|
COPY --from=builder /usr/src/microsoft-rewards-script/node_modules ./node_modules
|
||||||
|
|
||||||
# Default command runs the built-in scheduler; can be overridden by docker-compose
|
# Run the scheduled rewards script
|
||||||
CMD ["npm", "run", "start:schedule"]
|
CMD ["npm", "run", "start:schedule"]
|
||||||
3135
package-lock.json
generated
Normal file
3135
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user