Main Website
Web Scraping
Web Scraping
Updated on
May 21, 2024

Puppeteer in Docker: How to Run Examples & Error Troubleshoot

Puppeteer is a powerful Node.js library that enables developers to control headless Chrome or Chromium browsers for web scraping, automation and testing purposes. Running Puppeteer in Docker containers can help manage dependencies, simplify deployment, and ensure consistent performance across different environments.

In this article, we’ll discuss how to run Puppeteer in Docker, providing a step-by-step guide, and tips for optimizing performance. We’ll also cover common errors and troubleshooting techniques to help you overcome potential issues. By the end of this article, you’ll have a solid understanding of how to use Puppeteer in Docker containers, making your web automation projects more efficient and manageable.

Prerequisites: Setting up your environment

Before running Puppeteer in Docker, ensure you have the following prerequisites in place:

Create a new directory for your project

Start by creating a new directory on your system where you'll set up your Puppeteer project. You can create a new directory using the following command in your terminal:

mkdir puppeteer-docker-project

Initialize a new Node.js project

Navigate to the newly created directory and initialize a new Node.js project using npm (Node Package Manager). This will create a package.json file to manage your project dependencies.

cd puppeteer-docker-project
npm init -y

Install Puppeteer as a dependency

With your Node.js project initialized, you can now install Puppeteer as a dependency. Puppeteer is a Node.js library, so you'll need Node.js installed on your system to run Puppeteer scripts.

npm install puppeteer

Install Docker for containerization

To containerize your Puppeteer project, begin by downloading and installing Docker Desktop from the official Docker website. Once installed, verify the installation by running docker --version in your terminal or command prompt. Launch Docker Desktop to start the Docker service on your system, and optionally configure preferences such as resource allocation and network settings to suit your project requirements.

Set up Docker compose (Optional)

For managing multi-container applications and defining complex deployment configurations, consider setting up Docker Compose. Install Docker Compose if not already available, and create a docker-compose.yml file in your project directory. Define the services and configurations required for your project within the file, and then run Docker Compose using the command docker compose up. With Docker Compose, you can streamline the deployment and management of your Puppeteer project, especially in environments with multiple interconnected services.

How to run Puppeteer in Docker?

Now, let’s dive into the steps to run Puppeteer in Docker:

Create a Docker File

Begin by creating a Dockerfile in your project directory. The Dockerfile defines the environment for running Puppeteer inside the Docker container.

# Use the official Node.js 14 LTS image as the base image
FROM node:14-slim

# Set the working directory

# Copy the package.json and package-lock.json files
COPY package*.json ./

# Install the dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the default Puppeteer port

# Set the entrypoint script
ENTRYPOINT [ "node", "app.js" ]

Build Docker Image

Navigate to the directory containing your Dockerfile and execute the following command to build the Docker image:

docker build -t puppeteer-docker-example .

Run Docker Container

Once the Docker image is built, run a Docker container using the following command:

docker run -d puppeteer-docker-example

Verify Puppeteer execution

Verify that Puppeteer is running successfully inside the Docker container by executing your Puppeteer script within the container environment. Here’s an example to verify Puppeteer execution inside the Docker container:

const puppeteer = require('puppeteer');

(async () => {
  // Launch Puppeteer
  const browser = await puppeteer.launch();

  // Create a new page
  const page = await browser.newPage();

  // Navigate to a website
  await page.goto('');

  // Take a screenshot
  await page.screenshot({ path: 'example.png' });

  // Close Puppeteer
  await browser.close();

  console.log('Puppeteer executed successfully inside Docker container!');

To execute this code:

  1. Save the code in a file named app.js.
  2. Place the app.js file in the same directory as your Dockerfile and Node.js project files.
  3. Run the Docker container as described in the previous steps.
  4. Once the container is running, execute the app.js script inside the Docker container as shown: "docker exec -it <container_id> node app.js

Replace <container_id> with the ID of your running Docker container.

This code example launches Puppeteer, navigates to the Node.js website, takes a screenshot, and then closes Puppeteer. If Puppeteer executes successfully without errors, you'll see the message "Puppeteer executed successfully inside Docker container!" printed in the terminal.

Optimizing Puppeteer performance in Docker

Running Puppeteer in Docker environments offers numerous advantages, such as scalability, portability, and resource isolation. However, optimizing the performance of Puppeteer within Docker containers is essential to ensure efficient execution of browser automation tasks. In this section, we'll explore various strategies to optimize the performance of Puppeteer in Docker.

Minimize container size

  • Utilize Lightweight Base Images: Choose base images such as node:alpine or node:slim instead of full-fledged distributions to reduce image size.
  • Exclude Unnecessary Dependencies: Identify and exclude unnecessary dependencies and files from the Docker image to keep it lean. This can be achieved by leveraging .dockerignore files and carefully selecting which files to include in the image.

Use Headless mode

Enable Puppeteer's headless mode ({ headless: true }) to run the browser without a graphical user interface. Headless mode significantly reduces resource consumption and improves performance, making it ideal for Docker environments where GUIs are unnecessary.

Allocate sufficient resources

  • CPU and Memory Allocation: Ensure that Docker containers running Puppeteer are allocated sufficient CPU and memory resources to handle browser automation tasks effectively. Inadequate resource allocation can lead to performance bottlenecks and instability.
  • Docker Resource Constraints: Utilize Docker's resource constraint mechanisms (--cpus, --memory) to limit CPU and memory usage of containers and prevent resource contention in multi-container environments.

Optimize Dockerfile

  • Minimize Layers: Minimize the number of layers in the Dockerfile to reduce image build time and image size. Combine multiple commands into a single RUN instruction and avoid unnecessary intermediate images.
  • Use Multi-Stage Builds: Utilize multi-stage builds to separate build-time dependencies from runtime dependencies and create smaller final images.
  • Leverage Caching: Leverage Docker's build cache by arranging commands in the Dockerfile to maximize cache utilization. This helps speed up subsequent builds by reusing cached layers.

Browser launch options

  • Customize Launch Options: Customize Puppeteer's browser launch options to optimize performance in Docker containers. Experiment with parameters such as args, executablePath, and ignoreDefaultArgs to fine-tune browser behavior and resource utilization based on specific requirements.
  • Optimize Chromium Flags: Adjust Chromium flags passed to the browser to disable unnecessary features or enable performance-enhancing options. For example, disabling features like GPU acceleration --disable-gpu can improve performance in headless mode.

Container orchestration

  • Docker Swarm or Kubernetes: Consider deploying Puppeteer-based applications in container orchestration platforms like Docker Swarm or Kubernetes. These platforms offer features such as auto-scaling, load balancing, and resource management, which can enhance performance and reliability in production environments.
  • Horizontal Scaling: Utilize horizontal scaling techniques provided by container orchestration platforms to distribute workload across multiple containers and improve performance under high traffic conditions.

Monitor and tune performance

  • Docker Monitoring Tools: Use Docker monitoring tools like Docker Stats, Docker Events, and Docker Healthchecks to monitor container resource usage, performance metrics, and health status.
  • Application Performance Monitoring (APM): Implement APM solutions like Prometheus, Grafana, or Datadog to collect and analyze performance metrics at the application level. Monitor key metrics such as response time, throughput, and error rates to identify performance bottlenecks and optimize container configurations accordingly.

Troubleshooting common errors

Let's address common errors encountered when running Puppeteer in Docker and provide detailed troubleshooting steps for each:

Failed to launch the browser process

Description: This error occurs when Puppeteer is unable to initialize the browser process within the Docker container.


  • Missing Dependencies: The Docker environment may lack the necessary dependencies for launching the browser process, such as shared libraries or fonts.
  • Insufficient Permissions: The Docker container may not have sufficient permissions to access system resources required by the browser.

Possible Solutions:

  • Install Dependencies: Ensure that the Docker image includes all necessary dependencies for launching the browser process, such as libxss1, libasound2, and libatk1.
  • Set Permissions: Adjust the Docker container's permissions to grant necessary access to system resources. Use the --cap-add flag when running the container to add specific capabilities, or modify file permissions within the container as needed.

Page load timeout

Description: This error occurs when a webpage fails to load within the expected timeframe.


  • Slow Network Connection: A slow or unstable network connection can result in page load timeouts, especially when fetching resources from external domains.
  • Heavy Page Content: Pages with heavy content or numerous resources may take longer to load, leading to timeouts.

Possible Solutions:

  • Increase Timeout Settings: Adjust Puppeteer's timeout settings page.setDefaultNavigationTimeout() and page.setDefaultTimeout() to accommodate longer page load times. Consider setting longer timeouts for specific operations if necessary.
  • Optimize Page Content: Reduce the size and complexity of page content by optimizing images, scripts, and other resources. Minimize the number of external dependencies and prioritize critical content to improve load times.

Chromium binary not found

Description: This error arises when Puppeteer can not locate the Chromium binary necessary for browser operations within the Docker container.


  • Incorrect Path Configuration: Puppeteer may fail to locate the Chromium binary if the path is not configured correctly or if the binary is missing from the Docker image.

Possible Solutions:

  • Specify Binary Path: Explicitly specify the path to the Chromium binary using the executablePath option when launching Puppeteer (puppeteer.launch({ executablePath: '/path/to/chromium' })).
  • Include Chromium Binary: Ensure that the Docker image includes the Chromium binary or the necessary dependencies to download it during runtime. Use Puppeteer's puppeteer-core package if you prefer to use a system-installed Chromium binary.

Resource exhaustion

Description: This error occurs when the Docker container runs out of memory or CPU resources during browser operations.


  • Insufficient Memory Allocation: Docker containers may run out of memory when executing resource-intensive browser operations, resulting in crashes or slowdowns.
  • CPU Overload: High CPU usage within the Docker container can lead to resource exhaustion and degrade performance.

Possible Solutions:

  • Adjust Resource Limits: Increase the memory and CPU limits for the Docker container using Docker's resource constraint mechanisms (--memory, --cpus). Allocate additional resources to handle peak workloads and prevent resource exhaustion.
  • Optimize Browser Operations: Optimize Puppeteer scripts to minimize memory and CPU usage. Avoid performing unnecessary operations, implement efficient resource management techniques, and optimize code execution where possible.

Security restrictions

Description: This error arises when Docker security policies or configurations restrict Puppeteer’s access to certain resources.


  • Docker Security Policies: Docker security policies or container configurations may restrict Puppeteer's access to certain resources or functionalities, leading to errors.

Possible Solutions:

  • Adjust Docker Security Settings: Review Docker security settings and policies to ensure that Puppeteer has necessary permissions to access required resources and execute browser operations. Modify Docker configurations or security profiles as needed to relax restrictions and allow Puppeteer to function properly.

Connection refused or Network Errors

Description: This error occurs when Puppeteer encounters connection issues or network errors within the Docker container.


  • Network Configuration Issues: Docker container networking may be misconfigured or restricted, preventing Puppeteer from establishing connections to external resources or services.

Possible Solutions:

  • Verify Network Settings: Check Docker container networking settings and ensure that the container has access to required network resources. Verify firewall rules, network policies, and DNS configurations to identify and resolve network-related issues.

Puppeteer in Docker: Wrapping up

Running Puppeteer in Docker environments offers a flexible and scalable solution for browser automation tasks. By leveraging Docker’s containerization technology, you can easily deploy Puppeteer-based applications across various environments while ensuring consistency.

Throughout this article, we’ve explored the process of running Puppeteer in Docker, covering essential steps, optimizing performance, and troubleshooting common errors. By following best practices and addressing potential challenges, you can streamline the deployment and operation of Puppeteer applications within Docker containers.

Using Puppeteer on AWS Lambda for Scraping

Puppeteer Cluster: Basic & Advanced Setup Explained

Puppeteer Scraping: Get Started in 10 Minutes