symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.95k stars 223 forks source link

Unable to set up pather crawler with docker; E: Could not start chrome (or it crashed) #263

Open jankulma-turbine opened 5 years ago

jankulma-turbine commented 5 years ago

Dockerfile:

# https://github.com/symfony/panther#docker-integration
FROM php:latest

RUN apt-get update && apt-get install -y libzip-dev zlib1g-dev chromium && docker-php-ext-install zip
ENV PANTHER_NO_SANDBOX 1

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

docker-compose.yml:

version: "3"
services:
  crawler:
    build: .
    working_dir: /usr/src
    volumes:
      - .:/usr/src
    command: /bin/sh -c "/usr/local/bin/composer install && php index.php"

composer.json:

{
    "require": {
        "symfony/panther": "^0.6.0"
    }
}

index.php:

<?php

// https://github.com/symfony/panther#basic-usage
require __DIR__.'/vendor/autoload.php'; // Composer's autoloader

$client = \Symfony\Component\Panther\Client::createChromeClient();
$client->request('GET', 'https://api-platform.com'); // Yes, this website is 100% written in JavaScript
$client->clickLink('Support');

// Wait for an element to be rendered
$crawler = $client->waitFor('.support');

echo $crawler->filter('.support')->text();
$client->takeScreenshot('screen.png'); // Yeah, screenshot!

All files are in the same location. I run docker-compose build && docker-compose up and I'm getting following error: crawler_1 | Fatal error: Uncaught RuntimeException: Could not start chrome (or it crashed) after 30 seconds. in /usr/src/vendor/symfony/panther/src/ProcessManager/WebServerReadinessProbeTrait.php:51

This is similar to https://github.com/symfony/panther/issues/200, however in my case I'm not using panther for tests, but only to scrape, and I really don't know how to fix this. I think my problem might be related to invalid docker / docker-compose files.

Thanks a lot

zalexki commented 4 years ago

I do the same using panther to scrape and not testing, few days/weeks ago it started not working anymore, i thought it was like #262 but after a bunch of tries with different version here is my current setup that is working.

"symfony/panther": "^0.6.0",

Dockerfile :

FROM php:7.3-apache as base

RUN apt-get update && apt-get install -y \
  git \
  zip \
  libicu-dev \
  libzip-dev \
  wget \
  gnupg2 \
  libnss3 \
  libasound2 \
  fonts-liberation \
  libappindicator3-1 \
  xdg-utils \
  lsb-release \
  libxss1 \
  && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-configure zip --with-libzip
RUN docker-php-ext-install pdo pdo_mysql zip opcache
RUN docker-php-ext-enable opcache

COPY --from=composer:1.8.6 /usr/bin/composer /usr/bin/composer
RUN composer global require hirak/prestissimo

# Install Chrome for Panther
RUN wget https://www.slimjet.com/chrome/download-chrome.php?file=files%2F78.0.3904.97%2Fgoogle-chrome-stable_current_amd64.deb -O /chrome.deb
RUN dpkg -i /chrome.deb || apt-get install -yf
RUN rm /chrome.deb

# Set panther env variable to run in docker
ENV PANTHER_NO_SANDBOX=1

PHP:

$client = Client::createChromeClient(
            null,
            [
                '--window-size=1200,1100',
                '--headless',
                '--disable-dev-shm-usage',
                '--no-sandbox',
            ],
            [],
            null
        );
        $client->request('GET', $url);
        $client->followRedirects();