Introduction
Puppeteer is a Node.js library which provides a high-level API to control Chromium (or Firefox) browsers over the DevTools Protocol.
This guide helps to use Puppeteer inside a Docker container using the Node.js image.
If we use the Docker images for Node.js v14 LTS Gallium, when installing the chromium
package from apt
, it will be v90.0, which can have compatibility issues with the latest Puppeteer. This is because it was tested with the latest Chromium stable release.
Selecting the correct image
Well... we want to run a web browser inside a container. it's important to know what are the different between the available variants.
Alpine is enough but ...
Yeah, we can run Chromium using Alpine Linux, but we'll need a few extra steps to make it run. That's why we prefer Debian variants to make it easier.
Which distro?
Every major version of Node.js in built over a version of Debian, and that Debian version comes with an old version of Chromium, which one could be not compatible with the latest version of Puppeteer.
Node.js | Debian | Chromium |
---|---|---|
v14 | 9.13 | 73.0.3683.75 |
v16 | 10.9 | 90.0.4430.212 |
v17 | 11.2 | 99.0.4844.84 |
To quickly solve that issue we can use the Google Chrome's Debian package that always installs the latest stable version. Therefore, this Dockerfile is compatible with Node.js v14, v16, or any new one.
Why not the built-in Chromium
When we install Google Chrome, apt
will install all the dependencies for us.
Dockerfile
FROM node:slim AS app
# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install curl gnupg -y \
&& curl --location --silent 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.list' \
&& apt-get update \
&& apt-get install google-chrome-stable -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
# Install your app here...
💡 If you are in an ARM-based CPU like Apple M1, you should use the --platform
argument when you build the Docker image.
docker build --platform linux/amd64 -t image-name .
The code config
Remember to use the installed browser instead of the Puppeteer's built-in one inside your app's code.
import puppeteer from 'puppeteer';
...
const browser = await puppeteer.launch({
executablePath: '/usr/bin/google-chrome',
args: [...] // if we need them.
});
Conclusion
The browser installation via apt will resolve the required dependencies to run a headless browser inside a Docker container without any manual intervention. These dependencies are not included in the Node.js Docker images by default.
The easiest path to use Puppeteer inside a Docker container is installing Google Chrome because, in contrast to the Chromium package offered by Debian, Chrome only offers the latest stable version.
Update 2022-08-24
This new Dockerfile version
FROM node:slim
# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install gnupg wget -y && \
wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
apt-get update && \
apt-get install google-chrome-stable -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*
Applies the following changes:
A. Removes the apt-key
deprecation warning.
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
B. Uses wget
because it's installed by google-chrome-stable
and it reduces a few MiB not installing curl
.
Top comments (37)
After running the code and I got into multiple errors of browser not launching caused by using wrong executablePath etc, Below code help in resolving the issue
where is that
locateChrome
function tho?npmjs.com/package/locate-chrome here
Hi there, I know it's an old post, but it's still valid. I provide a config that works for image oraclelinux based on rh.
And the lunch:
Hi there,
I used your Dockerfile content along with mine as i am trying to to generate pdf file for this service that i'm building with typescript. now everything works locally but i can't deploy it to AWS as it exceeds the lambda limit. now i am trying to dockerize it and it get's deployed but throws the following error.
here is my Dockerfile
and here is my code
Hi! Here, you're using a multi-stage build in Docker. You are taken the
node:slim
image, installing puppeteer there. But then you started a new stage withFROM public.ecr.aws/lambda/nodejs:14
and you don't haveapt
or Chrome neither in this image because is based on Amazon Linux and it usesyum
as package manager (like RHEL).You can check some approaches like github.com/shelfio/chrome-aws-lamb... or github.com/alixaxel/chrome-aws-lambda that explains how to use pptr inside Lambdas.
Also, I found this here stackoverflow.com/a/66099373, but I didn't test it
i just tried this.
my Dockerfile
and my code
and i get this error
"Protocol error (Target.setAutoAttach): Target closed."
Any solution to this? Having the exact same error.
You should install all these X Window System dependencies in your Docker image:
alsa-lib
atk
cups-libs
ipa-gothic-fonts
libXcomposite
libXcursor
libXdamage
libXext
libXi
libXrandr
libXScrnSaver
libXtst
pango
xorg-x11-fonts-100dpi
xorg-x11-fonts-75dpi
xorg-x11-fonts-cyrillic
xorg-x11-fonts-misc
xorg-x11-fonts-Type1
xorg-x11-utils
Also, would have a look in here please? I'm so stuck!
stackoverflow.com/questions/737184...
updated my Dockerfile
i'm using your build and copying to my own build
but getting this error:
"Failed to launch the browser process! spawn /usr/bin/google-chrome ENOENT\n\n\nTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md\n"
Why aren't you using the Amazon ECS?
why? so it would workout with ECS but not with lambda?
ECS was made to work with Docker images and it fits with your needs. You can run Docker images on Lambdas but I didn't see that before, maybe you will pay more if you use Lambda because the price is based on CPU and RAM consumption.
The resources required to wake up a container are much more than just running and interpreting some code, so you could end up paying more just for resources that doesn't necessarily translate to performance.
I am trying this in ECS and getting thsi error
no valid OpenPGP data found.
can someone please help
@navarroaxel - nice article - helped me even though I am using the
python
versionThanks! It works fine for me.
Thank you for this, it got me unstuck. Do you know if installing the latest Google Chrome could lead to problems if using an older version of Puppeteer? How to avoid this?
Yep, puppeteer is tested with a specific version of Chromium, details here: pptr.dev/chromium-support.
Also in each release you can see that version: github.com/puppeteer/puppeteer/rel....
You can check the available version starting a container with:
And then these commands for
chromium
orgoogle-chrome-stable
:For
node:18-slim
you'll see this output:Just look for a puppeteer version that works fine with the given Chromium version.
This saved the day! :)
I made an account just to thank You.
I have been trying to run @unlighthouse and Puppeteer in a docker for two days now, with nothing working. I installed Chrome in twenty different ways with no luck; the puppeteer couldn't spawn Chrome. Miraculously this exact snipped worked! The dopamine rush and ecstasy were something indescribable.
how ridiculously hard is it to run puppeteer on a server. my mind is bending right now.
For anyone having troubles here is my solution (That runs on my machine lul)
Dockerfile
script.js
SOLVED If you're on an M1 mac you have to add
--platform linux/amd64
to your docker build command.When I run this same docker file I receive the following error:
The entire dockerfile is:
are u fixed this issue ??