How to create a docker image to run puppeteer? (Docker)

Harsh Vardhan Gautam
3 min readFeb 11, 2023

--

Have you ever used puppeteer? If you have, then you may also faced a situation which is like, you’re working on your local environment using puppeteer, it’s working fine but now comes a challenge to integrate this in your remote (maybe github or gitlab workflows) repository’s workflow. There, you must be wondering, how can we run puppeteer there, right?

Image with logos of puppeteer, nodejs, docker and chromium

If you’re searching for the answer of the above situation, then please continue reading the article. I suggest others also to continue because I’ve tried to explain it in such a way that you’ll definitely go with learning something.

Without further ado, let’s get started.

We’ll create a file named Dockerfile without any file extension.

Step 1: Define the base docker image. As we want to use puppeteer which runs on node, so we’ll use node:<version> image here.

FROM node:18

Step 2: Now, we have a base docker image which has node installed. Now, we’ll install the dependencies which are required for the puppeteer to run properly.

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/*

Step 3: Next, it’s all set to define commands as per your requirement. Or else you can simply create a docker image using our Dockerfile from below command.
Note: Please make sure to run following command from the directory where the Dockerfile is present.

docker build -t <image-name>:<tag-name> .

There you go, you can use the above generated docker image to run puppeteer. You can now integrate this docker image into your CI-CD pipeline to run maybe some end-to-end tests.

The complete Dockerfile is as follows:

FROM node:18

# 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/*

That’s it !!!

Thank you so much for taking out some time for yourself to learn something new. I really appreciate your decision.

I hope that the article will add some value to your time spent. I strongly believe in improvements, therefore please share your thooughts or feedback in the comments.

Thank You!

--

--