Docker 响应代码 500。消息:未知错误:找不到 chrome 二进制文件

Docker Response code 500. Message: unknown error: cannot find chrome binary

提问人:user16187251 提问时间:11/15/2023 最后编辑:user16187251 更新时间:11/16/2023 访问量:44

问:

我正在使用 selenium java 进行网络抓取。我有与我的 chrome 浏览器相同版本的 chrome 驱动程序,并且我还在我的代码中设置了 chrome 的二进制路径:

 ChromeOptions options = new ChromeOptions();
 options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");


 WebDriver driver = new ChromeDriver(options);

Springboot中的代码工作正常,我使用Dockerfile构建了一个docker映像:

FROM debian:bullseye-slim

WORKDIR /app

EXPOSE 8081

RUN apt-get update && apt-get install -y openjdk-17-jre-headless libnss3 libxcb1

COPY target/springboot-exe.jar /app/
COPY chromedriver.exe /app/

ENTRYPOINT ["java", "-jar", "/app/springboot-exe.jar"]

当我运行 docker 映像时,我收到错误 no chrome binary:

Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 23051
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
[1700010675.161][SEVERE]: bind() failed: Cannot assign requested address (99)
ChromeDriver was started successfully.
Exception in thread "Thread-5" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: no chrome binary at C:\Program Files\Google\Chrome\Application\chrome.exe
Host info: host: '32d4e279bec7', ip: '172.17.0.2'
Build info: version: '4.8.3', revision: 'e5e76298c3'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.16.3-microsoft-standard-WSL2', java.version: '17.0.9'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], binary: C:\Program Files\Google\Chr..., extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], binary: C:\Program Files\Google\Chr..., extensions: []}}}]
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:165)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:183)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
        at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:84)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:73)
        at com.linggd.demo.Duldung.run(Duldung.java:54)
        at java.base/java.lang.Thread.run(Thread.java:840)

我的Chrome浏览器版本是119.0.6045.160,并且 我的Chrome驱动程序版本是119.0.6045.105。 任何帮助都是值得赞赏的。

java docker google-chrome selenium-webdriver selenium-chromedriver

评论


答:

1赞 Cadence 11/15/2023 #1

Docker 容器使用与主计算机不同的文件系统布局。

你在代码中写道,

options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");

但是 chrome.exe 在 Docker 容器中不存在。

它也无助于将chrome.exe放入容器中,因为Docker容器运行Linux(在本例中为Debian Linux),而Linux无法运行exe文件。COPY


您应该做的是使用包管理器安装 Chrome 的副本供容器使用。这是我在自己的 Dockerfile 中安装依赖项所做的操作:

RUN apt-get install -y wget libgdk-pixbuf2.0-0 libnss3 libatk-bridge2.0-0 libcups2 libdrm2 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libxkbcommon-x11-0 libpangocairo-1.0-0 libasound2

然后,您可以通过将 Chrome 从其网站下载并安装到容器中来安装 Chrome 的 Linux 版本:

RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install -y ./google-chrome-stable_current_amd64.deb

我个人必须使用以下命令行参数运行 Chrome:

"--no-sandbox", "--disable-setuid-sandbox", "--headless", "--disable-gpu", "--remote-debugging-port=9222"

我假设 Chrome 安装在 上,因此 Java 应该能够自动检测 Chrome 二进制文件的位置,而无需您这样做。PATHsetBinary

评论

0赞 user16187251 11/16/2023
感谢您提供的信息,我已经更新了我的 Dockerfile,并且修复了 chrome 二进制错误。我保留了setBinary(),因为我不确定Chrome是否安装在路径上。当我运行 docker image 时,我收到另一个错误“无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败。“原因:org.openqa.selenium.WebDriverException:等待驱动程序进程启动超时。”我试图添加您建议的那些参数,但没有它们,但错误仍然存在。
0赞 Cadence 11/17/2023
我建议调查一下 Chrome 无法启动的原因。您可以通过在环境中尝试命令,或者在线搜索“docker selenium chrome issues”,或者在 SO 上提出新问题来做到这一点。docker exec