Spaces:
Paused
Paused
| ARG PLAYWRIGHT_BROWSERS_PATH=/ms-playwright | |
| # ------------------------------ | |
| # Base | |
| # ------------------------------ | |
| # Base stage: Contains only the minimal dependencies required for runtime | |
| # (node_modules and Playwright system dependencies) | |
| FROM node:22-bookworm-slim AS base | |
| ARG PLAYWRIGHT_BROWSERS_PATH | |
| ENV PLAYWRIGHT_BROWSERS_PATH=${PLAYWRIGHT_BROWSERS_PATH} | |
| # Set the working directory | |
| WORKDIR /app | |
| RUN --mount=type=cache,target=/root/.npm,sharing=locked,id=npm-cache \ | |
| --mount=type=bind,source=package.json,target=package.json \ | |
| --mount=type=bind,source=package-lock.json,target=package-lock.json \ | |
| --mount=type=bind,source=packages/playwright-mcp/package.json,target=packages/playwright-mcp/package.json \ | |
| npm ci --omit=dev && \ | |
| # Install system dependencies for playwright | |
| npx -y playwright-core install-deps chromium | |
| # ------------------------------ | |
| # Builder | |
| # ------------------------------ | |
| FROM base AS builder | |
| RUN --mount=type=cache,target=/root/.npm,sharing=locked,id=npm-cache \ | |
| --mount=type=bind,source=package.json,target=package.json \ | |
| --mount=type=bind,source=package-lock.json,target=package-lock.json \ | |
| --mount=type=bind,source=packages/playwright-mcp/package.json,target=packages/playwright-mcp/package.json \ | |
| npm ci | |
| # Copy the rest of the app | |
| COPY packages/playwright-mcp/*.json packages/playwright-mcp/*.js packages/playwright-mcp/*.ts . | |
| # ------------------------------ | |
| # Browser | |
| # ------------------------------ | |
| # Cache optimization: | |
| # - Browser is downloaded only when node_modules or Playwright system dependencies change | |
| # - Cache is reused when only source code changes | |
| FROM base AS browser | |
| RUN npx -y playwright-core install --no-shell chromium | |
| # ------------------------------ | |
| # Runtime | |
| # ------------------------------ | |
| FROM base | |
| ARG PLAYWRIGHT_BROWSERS_PATH | |
| ARG USERNAME=node | |
| ENV NODE_ENV=production | |
| ENV PLAYWRIGHT_MCP_OUTPUT_DIR=/tmp/playwright-output | |
| # Set the correct ownership for the runtime user on production `node_modules` | |
| RUN chown -R ${USERNAME}:${USERNAME} node_modules | |
| USER ${USERNAME} | |
| COPY --from=browser --chown=${USERNAME}:${USERNAME} ${PLAYWRIGHT_BROWSERS_PATH} ${PLAYWRIGHT_BROWSERS_PATH} | |
| COPY --chown=${USERNAME}:${USERNAME} packages/playwright-mcp/cli.js packages/playwright-mcp/package.json ./ | |
| # Run in headless and only with chromium (other browsers need more dependencies not included in this image) | |
| # HF Spaces requires port 7860 | |
| EXPOSE 7860 | |
| CMD ["node", "cli.js", "--headless", "--browser", "chromium", "--no-sandbox", "--port", "7860", "--host", "0.0.0.0", "--allowed-hosts", "*"] | |