1

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!

所有文件都在同一个位置。我跑了docker-compose build && docker-compose up,我收到以下错误: 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

这类似于https://github.com/symfony/panther/issues/200,但是在我的情况下,我没有使用 panther 进行测试,而只是为了刮,我真的不知道如何解决这个问题。我认为我的问题可能与无效的 docker / docker-compose 文件有关。

4

1 回答 1

2

我有同样的错误。我的解决方案是按照自述文件中的说明安装解压缩

“警告:在 *nix 系统上,必须安装 unzip 命令,否则您将遇到类似于 RuntimeException: sh: 1: exec: /app/vendor/symfony/panther/src/ProcessManager/../../chromedriver 的错误-bin/chromedriver_linux64:权限被拒绝(或 chromedriver_linux64:未找到)。根本原因是 PHP 的 ZipArchive 不保留 UNIX 可执行权限。

最后,重新安装 panther 库。

于 2019-12-12T21:41:19.133 回答