whatsapp-web.js: Can't run Whatsapp-web.js on Docker Container

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am trying to put my app inside a Docker Container. I am able to build it correctly after a lot of looking around but i am facing this issue.

Dockerfile

FROM node:18

RUN apt-get update \
    && apt-get install -y wget gnupg \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg \
    && sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-khmeros fonts-kacst fonts-freefont-ttf libxss1 \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* 
# Set the working directory
WORKDIR /app

# Copy your application files into the container
COPY . .

# Install your Node.js application dependencies
RUN npm install

# Build your application
RUN npm run build

# Expose the port your application runs on
EXPOSE 3000

# Start your Node.js application
CMD ["node", "dist/server/server.js"]

Expected behavior

Normal initialization of the Whatsapp-web.js Client code.

Steps to Reproduce the Bug or Issue

Run the normal example on Docker.

This is my build command:

docker build --platform linux/amd64 --no-cache -t whatsappclient:development .

Run command:

docker run --platform linux/amd64 -p 3000:3000 whatsappclient:development

Error Code:


/app/node_modules/whatsapp-web.js/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241
            reject(new Error([
                   ^

Error: Failed to launch the browser process!
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
[0928/234157.110661:ERROR:bus.cc(406)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0928/234157.303689:ERROR:scoped_ptrace_attach.cc(27)] ptrace: Function not implemented (38)
Assertion failed: p_rcu_reader->depth != 0 (/qemu/include/qemu/rcu.h: rcu_read_unlock: 101)


TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

    at onClose (/app/node_modules/whatsapp-web.js/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
    at ChildProcess.<anonymous> (/app/node_modules/whatsapp-web.js/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:232:79)
    at ChildProcess.emit (node:events:529:35)
    at ChildProcess._handle.onexit (node:internal/child_process:292:12)

Node.js v18.18.0

Browser Type

Google Chrome

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

“whatsapp-web.js”: “1.22.2-alpha.0” Node.js: 18

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Reactions: 1
  • Comments: 28 (1 by maintainers)

Most upvoted comments

Using Zenika Docker Image

Dockerfile with comments

# Use the Zenika image with Puppeteer support
FROM zenika/alpine-chrome:with-puppeteer

# Set working directory inside the container
WORKDIR /usr/src/app

# Switch to 'chrome' user before copying and installing
USER chrome

# Copy the package.json and package-lock.json for npm install
COPY --chown=chrome:chrome package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of your app files into the container
COPY --chown=chrome:chrome . .

# Expose the required ports
EXPOSE 3000

# Set the command to run your app. Adjust accordingly.
CMD ["node", "server.js"]

Dockerfile without comments

FROM zenika/alpine-chrome:with-puppeteer
WORKDIR /usr/src/app
USER chrome
COPY --chown=chrome:chrome package*.json ./
RUN npm install
COPY --chown=chrome:chrome . .
EXPOSE 3000
CMD ["node", "server.js"]

server.js

It’s not the whole file but the important bits.

const { Client, LocalAuth } = require("whatsapp-web.js");

const whatsapp = new Client({
  puppeteer: {
    headless: true,
    args: ["--no-sandbox"],
  },
  authStrategy: new LocalAuth(),
});

Build image and run

docker build -t james-whatsapp-test  .
docker run -p 3000:3000 james-whatsapp-test

Project Structure

Screenshot 2023-10-31 at 12 04 16 am

Other comments

  1. You can mount persisted volumes for auth and cache dir so that when you deploy new code you don’t have to login again.
  2. If you struggle to login you can send the QR code to yourself via for example Telegram or Email

Try this using the default browser

FROM node:latest

RUN apt-get update \
  && apt-get install -y \
  gconf-service \
  libgbm-dev \
  libasound2 \
  libatk1.0-0 \
  libc6 \
  libcairo2 \
  libcups2 \
  libdbus-1-3 \
  libexpat1 \
  libfontconfig1 \
  libgcc1 \
  libgconf-2-4 \
  libgdk-pixbuf2.0-0 \
  libglib2.0-0 \
  libgtk-3-0 \
  libnspr4 \
  libpango-1.0-0 \
  libpangocairo-1.0-0 \
  libstdc++6 \
  libx11-6 \
  libx11-xcb1 \
  libxcb1 \
  libxcomposite1 \
  libxcursor1 \
  libxdamage1 \
  libxext6 \
  libxfixes3 \
  libxi6 \
  libxrandr2 \
  libxrender1 \
  libxss1 \
  libxtst6 \
  ca-certificates \
  fonts-liberation \
  libappindicator1 \
  libnss3 \
  lsb-release \
  xdg-utils \
  && rm -rf /var/lib/apt/lists/*

# Set the working directory
WORKDIR /app

# Copy your application files into the container
COPY . .

# Install your Node.js application dependencies
RUN npm install

# Build your application
RUN npm run build

# Expose the port your application runs on
EXPOSE 3000

# Start your Node.js application
CMD ["node", "dist/server/server.js"]

Try this using the default browser

FROM node:latest

RUN apt-get update \
  && apt-get install -y \
  gconf-service \
  libgbm-dev \
  libasound2 \
  libatk1.0-0 \
  libc6 \
  libcairo2 \
  libcups2 \
  libdbus-1-3 \
  libexpat1 \
  libfontconfig1 \
  libgcc1 \
  libgconf-2-4 \
  libgdk-pixbuf2.0-0 \
  libglib2.0-0 \
  libgtk-3-0 \
  libnspr4 \
  libpango-1.0-0 \
  libpangocairo-1.0-0 \
  libstdc++6 \
  libx11-6 \
  libx11-xcb1 \
  libxcb1 \
  libxcomposite1 \
  libxcursor1 \
  libxdamage1 \
  libxext6 \
  libxfixes3 \
  libxi6 \
  libxrandr2 \
  libxrender1 \
  libxss1 \
  libxtst6 \
  ca-certificates \
  fonts-liberation \
  libappindicator1 \
  libnss3 \
  lsb-release \
  xdg-utils \
  && rm -rf /var/lib/apt/lists/*

# Set the working directory
WORKDIR /app

# Copy your application files into the container
COPY . .

# Install your Node.js application dependencies
RUN npm install

# Build your application
RUN npm run build

# Expose the port your application runs on
EXPOSE 3000

# Start your Node.js application
CMD ["node", "dist/server/server.js"]

It worked for me.

I’ll post my configs that’s working:

Dockerfile:

FROM node:18 WORKDIR /usr/src/app RUN apt-get update RUN apt-get install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD [“npm”, “start”]

Client instance:

             const client = new Client({
                            ...
                            puppeteer: {
                                headless: true,
                                args: ['--no-sandbox']
                            }
                        });

Hope it helps

This worked perfectly, thank you!

I’ll post my configs that’s working:

Dockerfile:

FROM node:18 WORKDIR /usr/src/app RUN apt-get update RUN apt-get install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD [“npm”, “start”]

Client instance:

             const client = new Client({
                            ...
                            puppeteer: {
                                headless: true,
                                args: ['--no-sandbox']
                            }
                        });

Hope it helps