puppeteer官方Docker镜像如何使用?-灵析社区

我是张工你呢

puppeteer官方Docker镜像:[https://pptr.nodejs.cn/guides/docker](https://link.segmentfault.com/?enc=AnfmLzTduR5bBUnSvVyycw%3D%3D.OYOLZxiap7E%2BO9UTvxFX6zj%2FKpPhh0fNsPPXtEDssN6BNANqYGHavfKiMSGH4H5x) 现在我的需求是使用`express`创建接口,当调用接口传递链接,再puppeteer访问链接进行截图,截图后把截图上传阿里Oss,返回上传阿里Oss后的图片链接 我的想法是再打包一次: ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250104/91ecdd52b97ba274db31f9af1d180daa.png) FROM ghcr.io/puppeteer/puppeteer 把官方镜像打包进行我们自己的Dockerfile,请大佬们这样可行吗?这个Dockerfile配置应该怎么写?请大佬指点下

阅读量:366

点赞量:17

问AI
Dockerfile FROM ghcr.io/puppeteer/puppeteer:latest RUN npm install express ali-oss COPY app.js . CMD node app.js app.js const express = require('express'); const puppeteer = require('puppeteer'); const OSS = require('ali-oss'); const app = express(); const port = 3000; app.use(express.json()); // Replace with your own OSS configuration const ossConfig = { accessKeyId: 'xxxx', accessKeySecret: 'xxxx', bucket: 'zzzzz', region: 'oss-cn-hangzhou' }; const ossClient = new OSS(ossConfig); app.get('/screenshot', async (req, res) => { const { url } = req.query; if (!url) { return res.status(400).json({ error: 'Missing "url" parameter.' }); } try { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto(url); const screenshot = await page.screenshot(); await browser.close(); // Generate a unique filename for the screenshot const filename = `screenshot-${Date.now()}.png`; console.log(filename) // Upload the screenshot to OSS const result = await ossClient.put(filename, screenshot); // Construct the OSS URL for the uploaded image const imageUrl = result.url; res.json({ imageUrl }); } catch (error) { console.error(error); res.status(500).json({ error: 'An error occurred while taking the screenshot and uploading it to OSS.' }); } }); app.listen(port, () => { console.log(`Server is running on port ${port}`); });