You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Docker Compose运行集成Selenium的SpringBoot应用失败求助

解决Docker Compose下Spring Boot + Selenium Firefox Debug容器无法建立会话的问题

我帮你分析下这个问题——单独跑两个容器都正常,但用Docker Compose一起启动就报错,核心问题基本出在容器间的网络通信或者启动时序上,下面是具体的排查和解决步骤:

1. 替换Selenium地址中的localhost为Compose服务名

单独运行时,你的Spring Boot应用在本地(或宿主机),用localhost:4444能访问宿主机映射的Selenium端口,但在Docker Compose的容器网络里,localhost指向容器自身,不是Selenium容器。

你需要把Spring Boot中配置的Selenium远程地址改成Compose里Selenium服务的名称,比如:

  • 如果你的Compose里Selenium服务叫selenium-firefox,那地址应该是http://selenium-firefox:4444/wd/hub
  • 建议通过环境变量传递这个地址,比如在Compose的Spring Boot服务里添加:
    environment:
      - SELENIUM_REMOTE_URL=http://selenium-firefox:4444/wd/hub
    
    然后在Spring Boot的配置文件(比如application.properties)里读取这个变量:
    selenium.remote.url=${SELENIUM_REMOTE_URL:http://localhost:4444/wd/hub}
    

2. 确保Spring Boot等Selenium完全就绪后再启动

Docker Compose默认会同时启动所有服务,可能你的Spring Boot应用启动完成时,Selenium容器还在初始化Firefox,这时请求就会失败。你可以通过depends_on结合healthcheck来解决这个问题:

示例Docker Compose配置:

version: '3.8'
services:
  selenium-firefox:
    image: selenium/standalone-firefox-debug:latest
    ports:
      - "4444:4444"
      - "5900:5900"
    healthcheck:
      # 检查Selenium服务是否就绪
      test: ["CMD", "curl", "-f", "http://localhost:4444/wd/hub/status"]
      interval: 10s  # 每10秒检查一次
      timeout: 5s    # 超时时间
      retries: 5     # 重试5次后判定为不健康
  spring-boot-app:
    build: .
    depends_on:
      selenium-firefox:
        condition: service_healthy  # 等待Selenium容器健康检查通过再启动
    environment:
      - SELENIUM_REMOTE_URL=http://selenium-firefox:4444/wd/hub

3. 排查Selenium容器的启动日志

如果上面两步还没解决,你可以查看Selenium容器的日志,确认浏览器是否正常启动:

docker-compose logs selenium-firefox

如果日志里有Firefox启动失败的信息,可能需要调整Selenium镜像的版本,或者检查容器的资源限制(比如内存不足)。

4. 确认容器在同一网络

Docker Compose默认会为所有服务创建一个专属网络,只要你的两个服务都在同一个Compose文件里,默认是互通的。如果手动指定了网络,要确保两个服务都加入同一个网络。

按照上面的步骤调整后,应该就能解决无法建立会话的问题了。

内容的提问来源于stack exchange,提问作者Adib Rajiwate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:20:31