From 23c691541db0a51b4765dd0943488def7741c0f4 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 29 Sep 2024 01:41:01 +0800 Subject: [PATCH] chore: docker add puppeteer support --- Dockerfile | 11 ++++++++++- docker-compose.yml | 3 +++ src/server/utils/env.ts | 1 + src/server/utils/screenshot/website.ts | 5 ++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 659df16..b094ed2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ COPY ./reporter/ ./reporter/ RUN apt update RUN cd reporter && go build . -# # Base ------------------------------ +# Base ------------------------------ FROM node:20-alpine AS base RUN npm install -g pnpm@9.7.1 @@ -32,6 +32,15 @@ RUN pnpm build:static FROM base AS app WORKDIR /app/tianji +# We don't need the standalone Chromium +ENV PUPPETEER_SKIP_DOWNLOAD=true +ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser + +RUN apk add --no-cache \ + udev \ + ttf-freefont \ + chromium + COPY . . RUN pnpm install --filter @tianji/server... --config.dedupe-peer-dependents=false diff --git a/docker-compose.yml b/docker-compose.yml index 6adf136..95e90c8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,9 @@ version: '3' services: tianji: image: moonrailgun/tianji + build: + context: ./ + dockerfile: ./Dockerfile ports: - "12345:12345" environment: diff --git a/src/server/utils/env.ts b/src/server/utils/env.ts index 2544cb3..99f2b3b 100644 --- a/src/server/utils/env.ts +++ b/src/server/utils/env.ts @@ -52,6 +52,7 @@ export const env = { sandboxMemoryLimit: process.env.SANDBOX_MEMORY_LIMIT ? Number(process.env.SANDBOX_MEMORY_LIMIT) : 16, // unit: MB + puppeteerExecutablePath: process.env.PUPPETEER_EXECUTABLE_PATH, dbDebug: checkEnvTrusty(process.env.DB_DEBUG), amapToken: process.env.AMAP_TOKEN, mapboxToken: process.env.MAPBOX_TOKEN, diff --git a/src/server/utils/screenshot/website.ts b/src/server/utils/screenshot/website.ts index a03e5b9..97689fc 100644 --- a/src/server/utils/screenshot/website.ts +++ b/src/server/utils/screenshot/website.ts @@ -10,7 +10,10 @@ export async function screenshotWebsiteDetailImage( websiteId: string, userId: string ) { - const browser = await puppeteer.launch({ headless: 'new' }); + const browser = await puppeteer.launch({ + executablePath: env.puppeteerExecutablePath, + headless: 'new', + }); const page = await browser.newPage(); try { const jwt = jwtSign({