Sure, you can absolutely use Puppeteer with Docker.
Puppeteer is a Node.js library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It's often used for web scraping or automating browser tasks.
To use Puppeteer with Docker, you'll need to do a few things:
Step 1: First, you'll need to create a Dockerfile. This will be used to create your Docker image. Here's an example of a Dockerfile you might use:
FROM node:10-slim
# Install Puppeteer dependencies
RUN apt-get update \
&& apt-get install -y wget gnupg ca-certificates \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable \
&& rm -rf /var/lib/apt/lists/*
# Install Puppeteer
RUN npm install puppeteer
WORKDIR /app
COPY . /app
Step 2: Then, you'll need to build your Docker image. You can do this with the docker build
command. Here's how you might do it:
docker build -t my-puppeteer-app .
Step 3: Once your Docker image has been built, you can run it with the docker run
command. Here's an example:
docker run -it --rm --cap-add=SYS_ADMIN my-puppeteer-app
A few notes about the above commands:
The
--cap-add=SYS_ADMIN
flag is necessary for Puppeteer to launch in Docker. Puppeteer needs certain system capabilities to launch Chrome or Chromium, andSYS_ADMIN
provides those capabilities.The
-it --rm
flags tell Docker to run the container interactively and remove it when it's done running.
Here's a simple Puppeteer script that you might run inside your Docker container:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
This script will open a new browser page, navigate to https://example.com
, take a screenshot, and save it as example.png
. Then it will close the browser.