Search code examples
dockersymfonysymfony-panther

How to set up panther crawler with docker


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.


Solution

  • I had the same error. My solution was install unzip as it says in the readme:

    "Warning: On *nix systems, the unzip command must be installed or you will encounter an error similar to RuntimeException: sh: 1: exec: /app/vendor/symfony/panther/src/ProcessManager/../../chromedriver-bin/chromedriver_linux64: Permission denied (or chromedriver_linux64: not found). The underlying reason is that PHP's ZipArchive doesn't preserve UNIX executable permissions."

    and finally, reinstall the panther library.