2

Deploying puppeteer on fly.io

 1 month ago
source link: https://willschenk.com/labnotes/2024/deploying_puppeteer_on_fly.io/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Test Script

  npm i puppeteer

screenshot.js

  import puppeteer from 'puppeteer';

  (async () => {
      const browser = await puppeteer.launch({
          headless: true,
          args: ['--no-sandbox', '--disable-setuid-sandbox']
      })
      
    const page = await browser.newPage();

    // Set the viewport's width and height
    await page.setViewport({ width: 1920, height: 1080 });

    // Open ScrapingBee's home page
    await page.goto('https://willschenk.com');

    try {
      // Capture screenshot and save it in the current folder:
      await page.screenshot({ path: `./willschenk.jpg` });

    } catch (err) {
      console.log(`Error: ${err.message}`);
    } finally {
      await browser.close();
      console.log(`Screenshot has been captured successfully`);
    }
  })();

Simple server

We aren't going to plug this into anything, just want to have something that fly can serve up.

  npm i express
  import express from "express";

  const app = express();
  const port = 3000;

  app.get('/', (req, res) => {
    res.send('Welcome to my server!');
  });

  app.get('/status', (req, res) => {
      
  });

  app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
  });

Dockerfile

Now the fun. First we install node and npm, then we install google-chrome-stable from google's repositories. We will download chrome again when we do npm i but this will take care of all the dependancies which are extensive.

  FROM --platform=linux/amd64 debian:bookworm-slim

  RUN apt-get update

  # Install node
  RUN apt-get install -y nodejs npm

  # Install chrome and dependencies
  RUN apt-get install -y wget gpg
  RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg \
      && sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
      && apt-get update
  RUN apt-get install -y google-chrome-stable fonts-freefont-ttf libxss1 \
      --no-install-recommends

  WORKDIR /app

  COPY package* .
  RUN npm i
  COPY * ./
  EXPOSE 3000
  CMD node app.js

Deploy

  fly launch
  fly ssh console

And inside of there, you should be able to run

  node screenshot.js 
  Screenshot has been captured successfully

Very meta

%3Cnil%3E


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK