How to Make Puppeteer Work in Docker: Both in Local and Production Environments

Stanislas Randriamilasoa
2 min readOct 7, 2023
Puppeteer + Docker

Setting up Puppeteer in Docker, particularly in the context of a Node.js application, can be a challenging task. I know, as I’ve found myself caught in the same struggle. The internet is full of how-to guides and tips, but finding a comprehensive, accurate, and reliable source can feel like searching for a needle in a haystack. Does it work on local? Does it function effectively in the production environment? Many times the examples we find online simply don’t fulfill all our needs.

In this article, I will show you how to make it work easily.

The environnement

  • Nodejs : 18.x
  • puppeteer: 19.11.1
  • Docker: 24.0.5

The Dockerfile

FROM node:18.13.0

RUN apt-get install -y python make gcc g++

# Install google-chrome-stable
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/*

# Create a user with name 'app' and group that will be used to run the app
RUN groupadd -r app && useradd -rm -g app -G audio,video app

WORKDIR /home/app

# Copy and setup your project

COPY package.json /home/app/package.json

COPY yarn.lock /home/app

RUN yarn install --frozen-lockfile

COPY . /home/app

RUN yarn build

EXPOSE 4000

# Give app user access to all the project folder
RUN chown -R app:app /home/app

RUN chmod -R 777 /home/app

USER app

CMD ["yarn", "start"]

Don’t forget to run the app with another user than default ‘root’, Chrome will not run properly with root user.

Your puppeteer code

const browser = await puppeteer.launch({
executablePath: '/usr/bin/google-chrome',
headless: 'new',
ignoreDefaultArgs: ['--disable-extensions'],
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

You can then build the docker image and run it in local env or in prod env.

Et voilà!

If you enjoyed this article, please give it a few claps you can leave up to 50 — or you can comment if you have any questions, I’ll do my best to answer them!

--

--