Docker容器在CentOS VPS服务器创建失败,Selenium Hub连接被拒
问题:Docker部署到CentOS VPS时无法连接selenium-hub:4444
本地环境下,通过docker-compose集成Selenium、Chrome Driver、MySQL搭建的网站后端可正常运行,但部署到CentOS系统的VPS服务器时,出现连接selenium-hub:4444被拒绝的错误,报错信息如下:
web_1 | crit: Microsoft.AspNetCore.Hosting.Diagnostics[6] web_1 | Application startup exception web_1 | OpenQA.Selenium.WebDriverException: An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://selenium-hub:4444/wd/hub/session. The exception message was: Connection refused (selenium-hub:4444) web_1 | ---> System.Net.Http.HttpRequestException: Connection refused (selenium-hub:4444) web_1 | ---> System.Net.Sockets.SocketException (111): Connection refused web_1 | at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) web_1 | at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) web_1 | at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|283_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
我的docker-compose配置文件:
version: "3" volumes: datafiles: services: database: image: mysql:8.0.28 depends_on: - selenium-hub ports: - "3307:3306" volumes: - datafiles:/var/lib/mysql - "~/sql-scripts/setup.sql:/docker-entrypoint-initdb.d/1.sql" restart: always environment: MYSQL_ROOT_PASSWORD: ***** MYSQL_USER: sa MYSQL_PASSWORD: ****** MYSQL_DATABASE: Pan chrome: image: selenium/node-chrome:4.3.0-20220706 shm_size: 2gb depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium-hub: image: selenium/hub:4.3.0-20220706 container_name: selenium-hub ports: - "4442:4442" - "4443:4443" - "4444:4444" web: build: . ports: - "8080:80" depends_on: - database - selenium-hub - chrome restart: always
创建Driver的C#代码片段:
ChromeOptions options = new ChromeOptions(); options.AddArgument("no-sandbox"); options.AddArgument("headless"); driver = new RemoteWebDriver(new Uri("http://selenium-hub:4444/wd/hub"), options);
解决方案
1. 解决服务启动顺序与就绪问题
depends_on仅保证容器启动顺序,不确保服务完全就绪。当web服务启动时,selenium-hub可能还在初始化,导致连接拒绝。
修改docker-compose配置,给selenium-hub添加健康检查,并让web服务等待selenium-hub就绪后再启动:
selenium-hub: image: selenium/hub:4.3.0-20220706 container_name: selenium-hub ports: - "4442:4442" - "4443:4443" - "4444:4444" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:4444/wd/hub/status"] interval: 10s timeout: 5s retries: 5 web: build: . ports: - "8080:80" depends_on: database: condition: service_started selenium-hub: condition: service_healthy chrome: condition: service_started restart: always
2. 检查CentOS的SELinux与防火墙设置
CentOS默认开启的SELinux可能会拦截容器间的通信,执行以下命令临时关闭测试(如需永久关闭需修改配置文件):
setenforce 0
如果问题解决,可通过以下命令允许Docker容器间通信:
setsebool -P docker_connect_any 1
同时检查防火墙是否拦截了容器端口,可临时关闭防火墙测试:
systemctl stop firewalld
确认没问题后,可按需开放端口或配置防火墙规则允许Docker网络通信。
3. 验证Selenium Hub与Chrome节点状态
查看selenium-hub日志,确认服务正常启动且Chrome节点已注册:
docker logs selenium-hub
若日志中无节点注册信息,检查chrome服务的环境变量是否正确,或尝试重启chrome容器:
docker-compose restart chrome
4. 优化ChromeOptions参数
在代码中添加--disable-dev-shm-usage参数,避免CentOS系统中共享内存不足的问题:
ChromeOptions options = new ChromeOptions(); options.AddArgument("no-sandbox"); options.AddArgument("headless"); options.AddArgument("--disable-dev-shm-usage"); // 添加此参数 driver = new RemoteWebDriver(new Uri("http://selenium-hub:4444/wd/hub"), options);
内容的提问来源于stack exchange,提问作者Sasan
相关产品推荐
相关产品推荐

