【问题标题】:Docker PHP7.x Codeception Selenium WebDriver throws net::ERR_CONNECTION_REFUSEDDocker PHP7.x Codeception Selenium WebDriver 抛出 net::ERR_CONNECTION_REFUSED
【发布时间】:2021-06-16 15:14:43
【问题描述】:

我正在尝试设置 Codeception 来为我的 Web 应用程序进行验收和功能测试。以下是我的文件:

docker-compose.yml

version: '3.7'

services:
  # nginx - web server
  nginx:
    build:
      context: ./docker-config/nginx
      dockerfile: ./Dockerfile
    env_file: &env
      - ./cms/.env
    init: true
    ports:
      - "8000:80"
    volumes:
      - cpresources:/var/www/project/cms/web/cpresources
      - ./cms/web:/var/www/project/cms/web:cached
    networks:
      mmc-network:
        aliases:
          - mmc.nginx
  # php - run php-fpm
  php:
    build: &php-build
      context: ./docker-config/php-prod-craft
      dockerfile: ./Dockerfile
    depends_on:
      - "mysql"
      - "redis"
    env_file:
      *env
    expose:
      - "9000"
    init: true
    volumes: &php-volumes
      - some volumes............
    networks:
      mmc-network:
        aliases:
          - mmc.php

  # mysql - database
  mysql:
    build:
      context: ./docker-config/mysql
      dockerfile: ./Dockerfile
    cap_add:
      - SYS_NICE  # CAP_SYS_NICE
    env_file:
      *env
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: project
      MYSQL_USER: project
      MYSQL_PASSWORD: project
    init: true
    ports:
      - "3306:3306"
    volumes:
      - db-data:/var/lib/mysql
      - ./db-seed/:/docker-entrypoint-initdb.d
    networks:
      - MMC-network

  # redis - key/value database for caching & php sessions
  redis:
    build:
      context: ./docker-config/redis
      dockerfile: ./Dockerfile
    expose:
      - "6379"
    init: true
    networks:
      - mmc-network

  # webpack - frontend build system
  webpack:
    build:
      context: ./docker-config/node-dev-webpack
      dockerfile: ./Dockerfile
    env_file:
      *env
    init: true
    ports:
      - "8080:8080"
    volumes:
      - some volumes..........
    networks:
      - mmc-network

  # selenium -  web driver for codeception testing
  selenium:
    container_name: mmc-selenium
    ports:
      - "4444:4444"
    volumes:
      - ./cms:/data
    build:
      context: ./docker-config/selenium
      dockerfile: ./Dockerfile
    networks:
      mmc-network:
        aliases:
          - mmc.selenium

volumes:
  db-data:
  cpresources:
  storage:

networks:
  mmc-network:

acceptance.suite.dist.yml:

actor: AcceptanceTester
extensions:
  enabled:
    - Codeception\Extension\RunFailed
    - Codeception\Extension\Recorder
modules:
  error_level: "E_ALL"
  enabled:
    - WebDriver:
        url: 'http://mmc.nginx' 
        host: mmc.selenium 
        port: '4444'
        window_size: 1920x1200 
        browser: 'chrome' 
        wait: 60 
        capabilities:
          os: Windows
          os_version: 10
          browserstack.local: true
    - \Helper\Acceptance

NavigationCept.php

<?php 
$I = new AcceptanceTester($scenario);

# define purpose of the test
$I->wantTo("check that navigation is functional.");

# check all menu items
$I->amOnPage('/');
$I->see('Search');

***现在,需要注意的是,Codeception 已经安装在我的 PHP 容器中并且工作正常。

当我尝试运行测试时,我收到以下错误,表明与我的主机(即我的 Nginx 服务器)的连接已被拒绝。

我尝试使用不同的url,例如https://google.com,它连接良好,一切顺利。我在这里做错了吗?我的url 参数不正确吗?如果可以的话,请帮助我。提前致谢。

【问题讨论】:

  • 你能用 --debug 标志运行测试并在此处复制输出吗
  • 你的 nginx 监听的是 8000 端口,而不是 80 端口。你如何通知客户端这件事? (对不起,对 WebDriver 参数一无所知)。 url: http://mmc.nginx:8000 不会帮忙吗?
  • 嗨,@OlesyaBolobova 我想我已经尝试过了,但不确定。我会尝试并让你知道。 :)
  • 我会通过进入 selenium 容器来调试它,看看你从那里得到了什么 docker-compose exec -it selenium bash curl http://mmc.nginx/ - 将输出添加到你的问题中。
  • @WilliamFrancisGomes 你能解决这个问题吗?

标签: php docker selenium nginx codeception


【解决方案1】:

我之前在 Codeception + Selenium 中看到过这样的异常。不过我没有那个项目配置文件可以和你分享。

我的强烈猜测是这个错误可能是由于围绕 Codeception-ChromeDriver-Selenium 版本的冲突

  • 您是否尝试在较低的 ChromeDriver 版本(例如 80 或 75)上执行此操作?
  • 或者更改 Selenium 版本?

您可以查看以下注释,这可能有助于您进行更多调试

如果我找到其他资源来解决此问题,将更新此答案。

【讨论】:

    【解决方案2】:

    Docker 服务可以相互访问using the service 名称作为主机名。所以你的配置可能是:

      enabled:
        - WebDriver:
            url: 'http://nginx' 
            host: selenium 
            port: '4444'
    

    一个服务的别名可以在另一个服务中指定为:

    selenium:
        links:
          - "nginx:mmc.nginx"
    

    那么url可以是url: 'http://mmc.nginx'

    解决方法:使用服务 IP 地址

      enabled:
        - WebDriver:
            url: 'http://172.18.0.3' 
            host: 172.18.0.2 
            port: '4444'
    

    使用busybox进行测试设置
    docker-compose.yaml 运行 2 个虚拟 Web 服务器,带有 netcat 实用程序,用于演示目的。

    version: '3.7'
    
    services:
      nginx:
        container_name: mmc-nginx
        hostname: mmc-nginx
        image: busybox
        command: ["sh", "-c", "while true ; do (echo -e 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-type: application/xml\r\n\r\n'; echo 'response from '`hostname`':80') | nc -l -p 80 ;done" ]
        init: true
        ports:
          - "8000:80"
        networks:
          - mmc-network
    
      # selenium -  web driver for codeception testing
      selenium:
        container_name: mmc-selenium
        hostname: mmc-selenium
        command: ["nc", "-l", "-p", "4444"]
        ports:
          - "4444:4444"
        image: busybox
        networks:
          - mmc-network
    
    networks:
      mmc-network:
        name: mmc-network
    

    测试命令:

    docker exec -it mmc-selenium wget -q "http://mmc-nginx" -O -
    docker exec -it mmc-selenium wget -q "http://172.18.0.3" -O -
    

    回复:

    response from mmc-nginx:80
    

    从外部 docker 网络访问:

    wget -q "http://localhost:8000" -O -
    
    response from mmc-nginx:80
    

    Ping 到mmc-nginx

    docker exec -it mmc-selenium ping -c2 mmc-nginx
    
    PING mmc-nginx (172.18.0.3): 56 data bytes
    64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.071 ms
    64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.111 ms
    
    --- mmc-nginx ping statistics ---
    2 packets transmitted, 2 packets received, 0% packet loss
    round-trip min/avg/max = 0.071/0.091/0.111 ms
    

    查找服务IP地址

    使用docker。相应地更改 svc 值。:

    svc='friendly_solomon' docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$svc"
    

    使用jq。相应地更改 svc 值。 jq 是一个很棒的 json 处理工具,顺便说一句。

    svc='mmc-nginx'; docker network inspect bridge | jq -r --arg svc "$svc" '.[] | .Containers | .[] | select(.Name == $svc) | .IPv4Address'
    

    使用grep

    svc='mmc-nginx' docker network inspect bridge | grep -A5 "$svc" | grep 'IPv4Address
    

    【讨论】:

    • 我都试过了,但恐怕没有帮助。 :(
    • 您是否尝试过在 selenium 上打开 bash 会话并 ping nginx 框?将在我的答案中添加一些内容。
    • 我看到你更新了代码。您在 yml 文件中编写的两个命令不太清楚,您能否解释一下 Nginx 容器的命令在做什么?谢谢。 :)
    • 这两个命令都会打开一个虚拟网络服务器以进行演示,因为我没有您的确切设置。这个想法是为了表明服务名称是 docker 中的主机名。在您的情况下,问题可能是如果您以非 root 用户身份运行安装程序,nginx 服务未打开端口 80。 ncnetcatutility。
    • 端口 80 实际上在 docker 内部网络上工作。将主机名设置为您想要的名称很重要:container_name: mmc-nginxdocker ps 应该显示实际的容器名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2012-10-21
    • 2017-07-23
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多