Pyppeteer and Docker Error: Browser closed unexpectedly:

2.8k views Asked by At

Receiving this error in a Docker and Python3.10 Container

An example link is as follow: https://finance.yahoo.com/quote/BABA/options?p=BABA&date=1653004800

Browser closed unexpectedly:

Here is my Dockerfile

FROM python:3.10

RUN apt-get update

# TA-Lib
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
  tar -xvzf ta-lib-0.4.0-src.tar.gz && \
  cd ta-lib/ && \
  ./configure && \
  make && \
  make install && \
  cd .. && \
  rm -R ta-lib ta-lib-0.4.0-src.tar.gz

COPY requirements.txt /tmp/
COPY data/stocks.csv /tmp/data/stocks.csv

RUN pip install --requirement /tmp/requirements.txt
RUN pip freeze >> /tmp/requirement.txt
RUN pyppeteer-install

CMD ["python", "/tmp/app.py"]
async def async_get_options_chain(ticker, date=None, raw=True, headers={'User-agent': 'Mozilla/5.0'}):

    """Extracts call / put option tables for input ticker and expiration date.  If
       no date is input, the default result will be the earliest expiring
       option chain from the current date.

       @param: ticker
       @param: date"""

    site = options.build_options_url(ticker, date)

    browser = await launch({'headless': True, 'options': {'args': ['--no-sandbox', '--disable-setuid-sandbox']}})
    page = await browser.newPage()

    await page.goto(site)
    content = await page.evaluate('document.body.textContent', force_expr=True)
2

There are 2 answers

1
Michael Paccione On

What appears to have solved this issue was not installing it from packages but rather from the Dockerfile. Now I have to solve a timeout issue instead :)

Dockerfile

# Pyppeteer
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    --no-install-recommends \
    && curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update && apt-get install -y \
    google-chrome-stable \
    --no-install-recommends

# It won't run from the root user.
RUN groupadd chrome && useradd -g chrome -s /bin/bash -G audio,video chrome \
    && mkdir -p /home/chrome && chown -R chrome:chrome /home/chrome

Python

    browser = await launch(executablePath='/usr/bin/google-chrome-stable', headless=True, args=['--no-sandbox'])
0
Ahoren On

I had the same problem and what solved it for me was indeed the docker file not having the correct requirements, but also, when I built the Docker, I needed to add --platform=linux/amd64. I'm running on an MB 16" M1 chip.

This solved it for me.
Docker file
## To run Pyppeteer and chromium
RUN apt-get install -y gconf-service \
    libasound2 \
    libatk1.0-0 \
    libatk-bridge2.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  \
    libcairo-gobject2 \
    libxinerama1 \
    libgtk2.0-0 \
    libpangoft2-1.0-0 \
    libthai0 \
    libpixman-1-0 \
    libxcb-render0 \
    libharfbuzz0b \
    libdatrie1 \
    libgraphite2-3 \
    libgbm1

RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" | tee /etc/apt/sources.list.d/chrome.list

And when I build the Docker, I add --platform=linux/amd64 like

docker build -f Dockerfile . -t docker-tag --platform=linux/amd64