Setting up Puppeteer in a Docker container for AWS Lambda: Installation help needed

Hey folks, I’m stuck with a tricky situation. I’m working on a TypeScript project that needs to create PDF files. It’s all good on my local machine, but when I try to deploy it to AWS Lambda, it goes over the size limit. So I thought, “Let’s containerize it!”

I managed to get it deployed using Docker, but now I’m hitting a new snag. The app crashes with an error about missing libraries. Here’s what I’m seeing:

Error: Can't launch browser!
/var/task/node_modules/puppeteer/.local-chromium/linux-1036745/chrome-linux/chrome: 
No libatk-1.0.so.0 found. Shared object file missing.

This happens when I try to start up Puppeteer:

let browser = await puppeteer.launch({
  headless: false,
  args: myArgs,
  ignoreDefaultArgs: ['--disable-extensions'],
  executablePath: '/usr/bin/chromium-browser',
});

let newPage = await browser.newPage();

I’ve included my Dockerfile, but I’m not sure what I’m missing. Any ideas on how to get Puppeteer playing nice with AWS Lambda in a Docker setup? Thanks in advance for any help!

hey there! i’ve run into similar issues before. have you tried installing the missing libraries in your Dockerfile? you might need to add something like:

RUN apt-get update && apt-get install -y libatk-1.0-0

also, make sure you’re using a compatible base image. alpine-based images can be tricky with puppeteer. good luck!

I’ve encountered this issue before when working with Puppeteer in Docker containers. One solution that worked for me was using the puppeteer-core package instead of the full puppeteer. This approach reduces the overall size and eliminates the need for bundled Chromium.

In your Dockerfile, you’ll need to install Chromium and its dependencies. Here’s a snippet you can add:

RUN apt-get update && apt-get install -y chromium chromium-l10n fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 --no-install-recommends

Then, in your code, use puppeteer-core and point to the installed Chromium:

import puppeteer from 'puppeteer-core';

const browser = await puppeteer.launch({
  executablePath: '/usr/bin/chromium-browser',
  args: ['--no-sandbox', '--disable-setuid-sandbox']
});

This approach should resolve your library issues and keep your container size manageable for AWS Lambda.

As someone who’s deployed Puppeteer in Docker containers on AWS Lambda, I can relate to your frustration. One thing that’s worked well for me is using the amazon/aws-lambda-nodejs base image. It comes pre-configured with most of the libraries Puppeteer needs.

In your Dockerfile, try something like this:

FROM amazon/aws-lambda-nodejs:14
RUN yum install -y amazon-linux-extras
RUN amazon-linux-extras install epel -y
RUN yum install -y chromium

This setup should provide the necessary dependencies without bloating your container too much. Also, make sure you’re using the --no-sandbox flag when launching Puppeteer:

let browser = await puppeteer.launch({
args: [‘–no-sandbox’, ‘–disable-setuid-sandbox’],
executablePath: ‘/usr/bin/chromium’,
});

This approach has saved me countless headaches. Hope it helps you too!