ConnectionRefusedError:Selenium Remote WebDriver from Python in Docker

zvms9eto  于 2024-01-06  发布在  Docker
关注(0)|答案(1)|浏览(168)

我尝试运行一个Flask应用程序,当某个端点被调用时,它会运行一个web scraper。所有内容都使用Docker Compose容器化。

Docker Compose File

  1. services:
  2. selenium:
  3. image: seleniarm/standalone-chromium # using this because I'm on M1
  4. hostname: local
  5. volumes:
  6. - '/dev/shm:/dev/shm'
  7. ports:
  8. - '4444:4444'
  9. api:
  10. build: api
  11. restart: always
  12. hostname: local
  13. environment:
  14. - environment=Local
  15. depends_on:
  16. - selenium
  17. ports:
  18. - '80:80'
  19. command: ["gunicorn", "-w", "3", "-t","300", "-b", "0.0.0.0:80", "app:app"]

字符串

API的Dockerfile

  1. FROM python:3.8
  2. COPY . /api
  3. WORKDIR /api
  4. RUN pip install -r requirements.txt --no-cache-dir
  5. EXPOSE 5000
  6. EXPOSE 4444 # not sure if this is necessary?

示例Python函数(这是由Flask路由调用的,我知道它配置正确

  1. from fake_useragent import UserAgent
  2. from selenium import webdriver
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.chrome.options import Options
  7. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  8. import time
  9. def test_webdriver():
  10. logger.info("Beginning to test webdriver")
  11. ua = UserAgent()
  12. user_agent = ua.random
  13. options = webdriver.ChromeOptions()
  14. options.add_argument('--log-level=3')
  15. options.add_argument('--verbose')
  16. options.add_argument('--ignore-ssl-errors=yes')
  17. options.add_argument('--ignore-certificate-errors')
  18. options.add_argument('--remote-debugging-port=9222')
  19. options.add_argument('--disable-dev-shm-usage')
  20. options.add_experimental_option("excludeSwitches", ['enable-logging'])
  21. options.add_argument(f'user-agent={user_agent}')
  22. driver = webdriver.Remote(
  23. command_executor="http://localhost:4444/wd/hub",
  24. options=options
  25. )
  26. driver.get('https://github.com')
  27. time.sleep(5)
  28. driver.quit()
  29. return


错误数:

  1. urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0xffffaff19490>: Failed to establish a new connection: [Errno 111] Connection refused


最终会抛出...

  1. urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xffffaff19490>: Failed to establish a new connection: [Errno 111] Connection refused'))


我假设由于某种原因,我的api容器无法访问我的selenium容器。我验证了访问http://localhost:4444会返回Selenium Grid UI,因此我知道容器已启动并正在运行。知道为什么API找不到它吗?

ifmq2ha2

ifmq2ha21#

这可能与你的Docker设置有关。你需要确保selenium和API服务可以访问同一个Docker网络。你可以通过在docker-compose文件的根级别添加:

  1. networks:
  2. flaskapp:
  3. driver: bridge

字符串
并且还将该网络添加到这两个服务,

  1. selenium:
  2. image: seleniarm/standalone-chromium
  3. hostname: local
  4. volumes:
  5. - '/dev/shm:/dev/shm'
  6. ports:
  7. - '4444:4444'
  8. networks:
  9. - flaskapp # This is what you need to add


对于您的API服务也是如此。

展开查看全部

相关问题