playwright-java在Docker内部的RuntimeException:无法创建驱动程序

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

playwright-java inside Docker RuntimeException: Failed to create driver

问题

在使用 Playwright-java(https://github.com/microsoft/playwright-java) 在 Java/Spring 中时,我遇到了异常(请参见下面的日志)。我理解我需要找到一种方法来查看运行容器的临时 .sh 文件,我希望 Java 应用程序使用 playwright-java 在 microsoft.com/playwright/java 容器内运行(版本为 v1.35.0)应该可以正常工作。

Dockerfile

FROM mcr.microsoft.com/playwright/java:v1.35.0-jammy-amd64
# 基于 Ubuntu 22.04 的镜像,具有 Java 17、mvn 3.8.8、chromium-1064、ffmpeg-1009、firefox-1408、webkit-1848
# 详细信息请参见 https://github.com/microsoft/playwright-java/blob/main/utils/docker/Dockerfile.jammy
# 对于标签和可用平台,请参见 https://mcr.microsoft.com/en-us/product/playwright/java/tags

WORKDIR /opt

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} ./company-pdf-service.jar

# 运行服务器
EXPOSE 8300
ENTRYPOINT ["java","-jar","./company-pdf-service.jar"]

请注意,我已经保留了您提供的代码和Dockerfile中的注释,并且没有对它们进行翻译。如果您需要进一步的帮助,请随时提出。

英文:

When using playwright-java https://github.com/microsoft/playwright-java
in Java/Spring

  @PostConstruct
  void init(){
    LOGGER.info("Initializinig Playwright with Chromium ...");
    Playwright playwright = Playwright.create();
    browser = playwright.chromium().launch(); // Page.pdf only supported in headless Chromium
  }

I get Exception (See log below). I understand that I need to find a way to look inside running container temp .sh file, I would expect Java app using playwright-java running inside microsoft.com/playwright/java container (of the same v1.35.0) should just work.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'playwrightPdfConverterService': Invocation of init method failed; nested exception is java.lang.RuntimeException: Failed to create driver
2023-06-14T15:05:31.174236127Z 	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
2023-06-14T15:05:31.174238335Z 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440)
2023-06-14T15:05:31.174240377Z 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)
2023-06-14T15:05:31.174242293Z 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
2023-06-14T15:05:31.174244168Z 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
2023-06-14T15:05:31.174246085Z 	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
2023-06-14T15:05:31.174250960Z 	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
2023-06-14T15:05:31.174286252Z 	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
2023-06-14T15:05:31.174289335Z 	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
2023-06-14T15:05:31.174291252Z 	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
2023-06-14T15:05:31.174293168Z 	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
2023-06-14T15:05:31.174296127Z 	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
2023-06-14T15:05:31.174298168Z 	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
2023-06-14T15:05:31.174300085Z 	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
2023-06-14T15:05:31.174302293Z 	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
2023-06-14T15:05:31.174304168Z 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
2023-06-14T15:05:31.174306002Z 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
2023-06-14T15:05:31.174307793Z 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
2023-06-14T15:05:31.174309543Z 	at com.company.pdf.Application.main(Application.java:12)
2023-06-14T15:05:31.174311293Z 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2023-06-14T15:05:31.174313085Z 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
2023-06-14T15:05:31.174314918Z 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2023-06-14T15:05:31.174316752Z 	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
2023-06-14T15:05:31.174318460Z 	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
2023-06-14T15:05:31.174320293Z 	at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
2023-06-14T15:05:31.174322085Z 	at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
2023-06-14T15:05:31.174323793Z 	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
2023-06-14T15:05:31.174325585Z Caused by: java.lang.RuntimeException: Failed to create driver
2023-06-14T15:05:31.174327293Z 	at com.microsoft.playwright.impl.driver.Driver.createAndInstall(Driver.java:105)
2023-06-14T15:05:31.174330835Z 	at com.microsoft.playwright.impl.driver.Driver.ensureDriverInstalled(Driver.java:57)
2023-06-14T15:05:31.174334460Z 	at com.microsoft.playwright.impl.PlaywrightImpl.createImpl(PlaywrightImpl.java:45)
2023-06-14T15:05:31.174340127Z 	at com.microsoft.playwright.impl.PlaywrightImpl.create(PlaywrightImpl.java:35)
2023-06-14T15:05:31.174343460Z 	at com.microsoft.playwright.Playwright.create(Playwright.java:110)
2023-06-14T15:05:31.174345335Z 	at com.microsoft.playwright.Playwright.create(Playwright.java:114)
2023-06-14T15:05:31.174347085Z 	at com.company.pdf.converter.PlaywrightPdfConverterService.init(PlaywrightPdfConverterService.java:29)
2023-06-14T15:05:31.174348835Z 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2023-06-14T15:05:31.174429543Z 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
2023-06-14T15:05:31.174448960Z 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2023-06-14T15:05:31.174451293Z 	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
2023-06-14T15:05:31.174453127Z 	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
2023-06-14T15:05:31.174455127Z 	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
2023-06-14T15:05:31.174457127Z 	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
2023-06-14T15:05:31.174458960Z 	... 26 common frames omitted
2023-06-14T15:05:31.174460460Z Caused by: java.io.IOException: Cannot run program "/tmp/playwright-java-7667674547892745859/playwright.sh": error=0, Failed to exec spawn helper: pid: 36, exit value: 1
2023-06-14T15:05:31.174462377Z 	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
2023-06-14T15:05:31.174464043Z 	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
2023-06-14T15:05:31.174465585Z 	at com.microsoft.playwright.impl.driver.jar.DriverJar.installBrowsers(DriverJar.java:93)
2023-06-14T15:05:31.174467252Z 	at com.microsoft.playwright.impl.driver.jar.DriverJar.initialize(DriverJar.java:69)
2023-06-14T15:05:31.174468877Z 	at com.microsoft.playwright.impl.driver.Driver.initialize(Driver.java:64)
2023-06-14T15:05:31.174472752Z 	at com.microsoft.playwright.impl.driver.Driver.createAndInstall(Driver.java:101)
2023-06-14T15:05:31.174475377Z 	... 39 common frames omitted
2023-06-14T15:05:31.174477002Z Caused by: java.io.IOException: error=0, Failed to exec spawn helper: pid: 36, exit value: 1
2023-06-14T15:05:31.174478668Z 	at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
2023-06-14T15:05:31.174480335Z 	at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:314)
2023-06-14T15:05:31.174482127Z 	at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:244)
2023-06-14T15:05:31.174496252Z 	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
2023-06-14T15:05:31.174498960Z 	... 44 common frames omitted
2023-06-14T15:05:31.174500585Z 

Dockerfile

FROM mcr.microsoft.com/playwright/java:v1.35.0-jammy-amd64
# Ubuntu 22.04-based image with java 17, mvn 3.8.8
# and chromium-1064  ffmpeg-1009  firefox-1408  webkit-1848
# See https://github.com/microsoft/playwright-java/blob/main/utils/docker/Dockerfile.jammy
# For tags and available platform see
# https://mcr.microsoft.com/en-us/product/playwright/java/tags

WORKDIR /opt

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} ./company-pdf-service.jar



# run server
EXPOSE 8300
ENTRYPOINT ["java","-jar","./company-pdf-service.jar"]

答案1

得分: 0

请参考这个框架:https://github.com/susnigdha1/PlaywrightTestAutomationFramework,我已经添加了如何使用Spring Boot、Cucumber、JUnit5、Maven和Docker的示例。

英文:

please refer to this framework: https://github.com/susnigdha1/PlaywrightTestAutomationFramework, I have added example of how to use Springboot, Cucumber, Junit5, Maven and Docker.

答案2

得分: 0

问题出在我试图在基于ARM架构的苹果硅芯片的M1 MacBook上运行linux/amd64镜像。

一旦切换到使用ARM架构运行一切:linux/arm64,问题就消失了。确切的原因不清楚,但很可能是纯JDK的问题。

关于使用Docker镜像的一些建议:

设置环境变量DEBUG=pw:install会提供与浏览器安装和连接相关的更多Playwright日志。

ARG BASE_IMAGE_ARCH=amd64
FROM mcr.microsoft.com/playwright/java:v1.35.0-jammy-${BASE_IMAGE_ARCH}

...

ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=true
ENV DEBUG=pw:install

有了这样的Dockerfile,仍然很容易尝试arm64和amd64两者。

英文:

The problem was because I was trying to run linux/amd64 images on M1 mac
(Apple MacBook with ARM-based Apple Silicon).

Once switchedc to run everything as ARM architecture: linux/arm64, the problem went away. The exact cause is unclear, but most likely pure JDK issue.

Some hints for using docker images:

Having envvar DEBUG=pw:install gives more Playwright logging related to browser install and connection.

ARG BASE_IMAGE_ARCH=amd64
FROM mcr.microsoft.com/playwright/java:v1.35.0-jammy-${BASE_IMAGE_ARCH}

...

ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=true
ENV DEBUG=pw:install

With such Dockerfile it is still easy to try both arm64 and amd64

huangapple
  • 本文由 发表于 2023年6月14日 23:28:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475219.html
匿名

发表评论

匿名网友

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

确定