Spaces:
Paused
Paused
kanchi commited on
chore(docker): optimize Dockerfile by excluding unnecessary files and using non-root user (#273)
Browse files- Dockerfile +56 -9
Dockerfile
CHANGED
|
@@ -1,22 +1,69 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Set the working directory
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Copy the rest of the app
|
| 16 |
-
COPY . .
|
|
|
|
| 17 |
|
| 18 |
# Build the app
|
| 19 |
RUN npm run build
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Run in headless and only with chromium (other browsers need more dependencies not included in this image)
|
| 22 |
-
ENTRYPOINT ["node", "cli.js", "--headless", "--browser", "chromium"]
|
|
|
|
| 1 |
+
ARG PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
|
| 2 |
+
|
| 3 |
+
# ------------------------------
|
| 4 |
+
# Base
|
| 5 |
+
# ------------------------------
|
| 6 |
+
# Base stage: Contains only the minimal dependencies required for runtime
|
| 7 |
+
# (node_modules and Playwright system dependencies)
|
| 8 |
+
FROM node:22-bookworm-slim AS base
|
| 9 |
+
|
| 10 |
+
ARG PLAYWRIGHT_BROWSERS_PATH
|
| 11 |
+
ENV PLAYWRIGHT_BROWSERS_PATH=${PLAYWRIGHT_BROWSERS_PATH}
|
| 12 |
|
| 13 |
# Set the working directory
|
| 14 |
WORKDIR /app
|
| 15 |
|
| 16 |
+
RUN --mount=type=cache,target=/root/.npm,sharing=locked,id=npm-cache \
|
| 17 |
+
--mount=type=bind,source=package.json,target=package.json \
|
| 18 |
+
--mount=type=bind,source=package-lock.json,target=package-lock.json \
|
| 19 |
+
npm ci --omit=dev && \
|
| 20 |
+
# Install system dependencies for playwright
|
| 21 |
+
npx -y playwright-core install-deps chromium
|
| 22 |
|
| 23 |
+
# ------------------------------
|
| 24 |
+
# Builder
|
| 25 |
+
# ------------------------------
|
| 26 |
+
FROM base AS builder
|
| 27 |
|
| 28 |
+
RUN --mount=type=cache,target=/root/.npm,sharing=locked,id=npm-cache \
|
| 29 |
+
--mount=type=bind,source=package.json,target=package.json \
|
| 30 |
+
--mount=type=bind,source=package-lock.json,target=package-lock.json \
|
| 31 |
+
npm ci
|
| 32 |
|
| 33 |
# Copy the rest of the app
|
| 34 |
+
COPY *.json *.js *.ts .
|
| 35 |
+
COPY src src/
|
| 36 |
|
| 37 |
# Build the app
|
| 38 |
RUN npm run build
|
| 39 |
|
| 40 |
+
# ------------------------------
|
| 41 |
+
# Browser
|
| 42 |
+
# ------------------------------
|
| 43 |
+
# Cache optimization:
|
| 44 |
+
# - Browser is downloaded only when node_modules or Playwright system dependencies change
|
| 45 |
+
# - Cache is reused when only source code changes
|
| 46 |
+
FROM base AS browser
|
| 47 |
+
|
| 48 |
+
RUN npx -y playwright-core install --no-shell chromium
|
| 49 |
+
|
| 50 |
+
# ------------------------------
|
| 51 |
+
# Runtime
|
| 52 |
+
# ------------------------------
|
| 53 |
+
FROM base
|
| 54 |
+
|
| 55 |
+
ARG PLAYWRIGHT_BROWSERS_PATH
|
| 56 |
+
ARG USERNAME=node
|
| 57 |
+
ENV NODE_ENV=production
|
| 58 |
+
|
| 59 |
+
# Set the correct ownership for the runtime user on production `node_modules`
|
| 60 |
+
RUN chown -R ${USERNAME}:${USERNAME} node_modules
|
| 61 |
+
|
| 62 |
+
USER ${USERNAME}
|
| 63 |
+
|
| 64 |
+
COPY --from=browser --chown=${USERNAME}:${USERNAME} ${PLAYWRIGHT_BROWSERS_PATH} ${PLAYWRIGHT_BROWSERS_PATH}
|
| 65 |
+
COPY --chown=${USERNAME}:${USERNAME} cli.js package.json ./
|
| 66 |
+
COPY --from=builder --chown=${USERNAME}:${USERNAME} /app/lib /app/lib
|
| 67 |
+
|
| 68 |
# Run in headless and only with chromium (other browsers need more dependencies not included in this image)
|
| 69 |
+
ENTRYPOINT ["node", "cli.js", "--headless", "--browser", "chromium"]
|