Editing
Dockerized Screenshot API using Puppeteer
(section)
From H4KS
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Dockerized Screenshot API using Puppeteer == This guide explains how to set up a simple screenshot API using Puppeteer in a Docker container. === Step 1: Create a Dockerfile === Create a new directory for your project and create a file named '''Dockerfile''' in that directory with the following content: <pre> # Use the official Node.js image FROM node:14 # Set the working directory WORKDIR /app # Copy package.json and package-lock.json COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of the application code COPY . . # Expose the port EXPOSE 3000 # Start the application CMD ["node", "server.js"] </pre> === Step 2: Create a package.json === In the same directory, create a '''package.json''' file with the following content: <pre> { "name": "screenshot-api", "version": "1.0.0", "main": "server.js", "dependencies": { "express": "^4.17.1", "puppeteer": "^10.0.0" } } </pre> === Step 3: Create the server code === Create a file named '''server.js''' in the same directory with the following content: <pre> const express = require('express'); const puppeteer = require('puppeteer'); const app = express(); const PORT = 3000; app.get('/screenshot', async (req, res) => { const url = req.query.url; if (!url) { return res.status(400).send('URL is required'); } try { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto(url); const screenshot = await page.screenshot(); await browser.close(); res.set('Content-Type', 'image/png'); res.send(screenshot); } catch (error) { console.error(error); res.status(500).send('Error taking screenshot'); } }); app.listen(PORT, () => { console.log(`Screenshot API running at http://localhost:${PORT}`); }); </pre> === Step 4: Build and Run the Docker Container === # Open a terminal and navigate to the directory where you created the '''Dockerfile''', '''package.json''', and '''server.js'''. # Build the Docker image: <pre> docker build -t screenshot-api . </pre> # Run the Docker container: <pre> docker run -p 3000:3000 --shm-size=1gb screenshot-api </pre> === Step 5: Use the Screenshot API === Once the container is running, you can take screenshots by making a GET request to the '''/screenshot''' endpoint with a '''url''' query parameter. For example: <pre> curl "http://localhost:3000/screenshot?url=https://example.com" --output screenshot.png </pre> This will save the screenshot of the specified URL as '''screenshot.png'''. === Notes === * Make sure you have Docker installed on your machine. * The '''--shm-size=1gb''' option is important for Puppeteer to work correctly, as it requires shared memory for rendering. * You can customize the server code further to add more features, such as error handling, different image formats, etc.
Summary:
Please note that all contributions to H4KS are considered to be released under the Creative Commons Attribution (see
H4KS:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Page actions
Page
Discussion
Read
Edit
History
Page actions
Page
Discussion
More
Tools
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Search
Tools
What links here
Related changes
Special pages
Page information