selenium/standalone-chrome:latest 找不到服务名称解决办法

selenium/standalone-chrome:latest 找不到服务名称解决办法

问题概述:

使用 docker-compose 创建和管理容器,使用镜像selenium/standalone-chrome:latest 创建容器 chrome

docker-compose文件如下:

version: '3.6'

services:
  whatweb:
    image: whatweb:v0.0.2
    build:
      context: ./services/whatweb
      dockerfile: Dockerfile-information
    volumes:
      - './services/whatweb:/usr/src/app'
    restart: always
    env_file:
      - whatweb-information.env
    network_mode: host

  whatweb-chrome:
    image: selenium/standalone-chrome:latest
    restart: always
    shm_size: 2g
    network_mode: host

配置文件如下:

# chromedriver_url
COMMAND_EXECUTOR=http://chrome:4444/wd/hub  # 此处的chrome 为容器名称

当启动容器时并没有问题,可能正常使用 4444 端口,但是一旦调用配置文件中的信息就会出现问题

raised unexpected: MaxRetryError(\"HTTPConnectionPool(host='whatweb-chrome', port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f02296f2e90>: Failed to establish a new connection: [Errno -2] Name or service not known'))\")",

此时是因为在docker-compose 文件中我们配置的 network 为 host模式,我们是直接使用宿主机的网络配置,并没有出现chrome 与 ip 地址的映射关系,所以服务并不能识别访问http://chrome:4444/wd/hub

解决办法

# chromedriver_url
COMMAND_EXECUTOR=http://127.0.0.1:4444/wd/hub  # 将 Chrome 直接替换为 localhost 地址,会直接调用本机网络配置去访问

猜你喜欢

转载自www.cnblogs.com/zhangweijie01/p/12097480.html