Playwright 无法在 Alpine Docker 上启动 Chrome。

huangapple go评论146阅读模式
英文:

Playwright is unable to launch chrome on Alpine Docker

问题

以下是您提供的内容的中文翻译:

在使用Docker中的node:lts-alpine运行Node.js应用程序时,我遇到了以下错误。

  1. INFO PlaywrightCrawler: Starting the crawl
  2. WARN PlaywrightCrawler: Reclaiming failed request back to the list or queue. browserType.launchPersistentContext: Failed to launch: Error: spawn /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome ENOENT
  3. =========================== logs ===========================
  4. <launching> /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome --disable-field-trial-config --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --proxy-server=http://127.0.0.1:43519 --proxy-bypass-list=<-loopback> --disable-blink-features=AutomationControlled --user-data-dir=/tmp/playwright_chromiumdev_profile-oKPLgl --remote-debugging-pipe about:blank
  5. [pid=N/A] starting temporary directories cleanup
  6. [pid=N/A] finished temporary directories cleanup
  7. ============================================================
  8. {"id":"Gl0EwOcnElHCOkr","url":"https://labs.withgoogle.com/","retryCount":1}

这是我的代码:

  1. const scrapeWebsiteUsingApify = async (source) => {
  2. const { Actor } = Apify;
  3. const { PlaywrightCrawler } = Crawlee;
  4. try {
  5. const sourceKey = source.replace(/[^a-zA-Z0-9]/g, '');
  6. await Actor.init();
  7. const store = await Actor.openKeyValueStore();
  8. // 检查给定源URL的数据是否存在于存储中
  9. const record = await store.getValue(sourceKey);
  10. if (record) {
  11. // 如果数据存在于存储中,则直接返回它
  12. return record;
  13. }
  14. // 如果数据不存在于存储中,则爬取网站
  15. let content;
  16. const crawler = new PlaywrightCrawler({
  17. async requestHandler({ page }) {
  18. await page.waitForTimeout(3000);
  19. // 作为结果返回数据
  20. content = await page.content();
  21. },
  22. });
  23. const crawledInfo = await crawler.run([source]);
  24. // 将爬取的数据存储在键值存储中以备将来使用
  25. await store.setValue(sourceKey, { ...crawledInfo, content });
  26. return { ...crawledInfo, content };
  27. } catch (e) {
  28. return null;
  29. }
  30. };

该代码在我的M1 MacBook Pro上运行正常,但在部署时无法启动Chrome。我验证了以下位置:

  1. /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome

它具有以下权限的chrome文件,

  1. -rwxr-xr-x 1 root root 372244488 May 10 13:45 chrome

从错误中可以很清楚地看出它无法定位文件,因此我也检查了$PATH并尝试将chrome的路径添加到其中,但即使这样也似乎不起作用。

您能帮助我理解这个错误并提供可能的修复方法吗?

英文:

I am getting the following error when running scrapper (nodejs application) on node:lts-alpine in docker.

  1. INFO PlaywrightCrawler: Starting the crawl
  2. WARN PlaywrightCrawler: Reclaiming failed request back to the list or queue. browserType.launchPersistentContext: Failed to launch: Error: spawn /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome ENOENT
  3. =========================== logs ===========================
  4. <launching> /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome --disable-field-trial-config --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --proxy-server=http://127.0.0.1:43519 --proxy-bypass-list=<-loopback> --disable-blink-features=AutomationControlled --user-data-dir=/tmp/playwright_chromiumdev_profile-oKPLgl --remote-debugging-pipe about:blank
  5. [pid=N/A] starting temporary directories cleanup
  6. [pid=N/A] finished temporary directories cleanup
  7. ============================================================
  8. {"id":"Gl0EwOcnElHCOkr","url":"https://labs.withgoogle.com/","retryCount":1}

Here is my code:

  1. const scrapeWebsiteUsingApify = async (source) => {
  2. const { Actor } = Apify;
  3. const { PlaywrightCrawler } = Crawlee;
  4. try {
  5. const sourceKey = source.replace(/[^a-zA-Z0-9]/g, '');
  6. await Actor.init();
  7. const store = await Actor.openKeyValueStore();
  8. // Check if data for the given source URL exists in the store
  9. const record = await store.getValue(sourceKey);
  10. if (record) {
  11. // If data exists in the store, return it directly
  12. return record;
  13. }
  14. // If data does not exist in the store, scrape the website
  15. let content;
  16. const crawler = new PlaywrightCrawler({
  17. async requestHandler({ page }) {
  18. await page.waitForTimeout(3000);
  19. // Return the data as a result
  20. content = await page.content();
  21. },
  22. });
  23. const crawledInfo = await crawler.run([source]);
  24. // Store the scraped data in the key-value store for future use
  25. await store.setValue(sourceKey, { ...crawledInfo, content });
  26. return { ...crawledInfo, content };
  27. } catch (e) {
  28. return null;
  29. }
  30. };

The code works fine on my M1 macbook pro, but on deployment it fails to launch chrome. I verified the location

  1. /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome

It has the chrome file with the following permissions,

  1. -rwxr-xr-x 1 root root 372244488 May 10 13:45 chrome

It is quite clear from the error that it's not able to locate the file so checked the $PATH as well and tried adding the path of the chrome to it, but even that doesn't seem to work.

Can you help me understand the error and a possible fix would be much appreciated.

答案1

得分: 2

Playwright在开箱即用时无法处理该图像。

相关Playwright问题:https://github.com/microsoft/playwright/issues/2826

您观察到的错误是由于缺少依赖项引起的。您可以通过登录到容器并运行ldd /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome来检查。

Playwright有一个名为'install-dependencies'的命令来解决此问题,但它使用的是apt-get,而在alpine中不可用(alpine使用apk)。

还有来自Playwright团队的docker镜像,以及可以通过在alpine镜像上专门安装chrome的alpine-chrome镜像。

英文:

Playwright doesn't work with that image out of the box.

Related Playwright issue: https://github.com/microsoft/playwright/issues/2826

The error you observe is because of missing dependencies. You can check that by logging in to container and running ldd /root/.cache/ms-playwright/chromium-1060/chrome-linux/chrome.

Playwright has 'install-dependencies' command to fix this issue, but it works with apt-get which is not available in alpine (alpine uses apk).

There are also docker images from playwright team as well as there is alpine-chrome image that works by installing chrome specifically over alpine image.

答案2

得分: 0

你应该使用来自 Apify 的专用图像 - https://github.com/apify/actor-templates/blob/master/templates/js-crawlee-playwright-chrome/.actor/Dockerfile

英文:

You should use the dedicated images from Apify - https://github.com/apify/actor-templates/blob/master/templates/js-crawlee-playwright-chrome/.actor/Dockerfile

huangapple
  • 本文由 发表于 2023年5月11日 16:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定