Docker PHP7.x Codeception Selenium WebDriver throws net::ERR_CONNECTION_REFUSED

Viewed 436

I am trying to set up Codeception to do Acceptance and Functional testing for my web app. Below are my files:

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');

***Now, point to be noted, Codeception is already installed inside my PHP container and working perfectly.

When I try to run the test, I get the below error which indicates that the connection to my host (which is my Nginx server) has been refused. enter image description here

I tried with a different url, for example, https://google.com and it just connected fine and everything was successful. Am I doing something wrong in here? Is my url param incorrect? Please help me out if you can. Thanks in advance.

2 Answers

Docker service are reachable from each other using the service name as hostname. So your configuration could be:

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

An alias to a service could be specified at another service as:

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

Then the url could be url: 'http://mmc.nginx'

Workaround: use service IP address

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

Test setup using busybox:
docker-compose.yaml that runs 2 dummy web servers with netcat utility for demonstration purposes.

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

test command:

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:

response from mmc-nginx:80

Access from outside docker network:

wget -q "http://localhost:8000" -O -

response from mmc-nginx:80

Ping to 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

Find service IP address:

Using docker. Change svc value accordingly.:

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

Using jq. Change svc value accordingly. jq is a great json processing tool to have btw.

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

Using grep

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

I have seen such exception earlier with Codeception + Selenium. I do not have that project config file to share with you though.

My strong guess here is this error could be due to conflicts around Codeception-ChromeDriver-Selenium versions

  • Have you tried to execute this on a lower ChromeDriver version say 80 or 75?
  • Or change the Selenium version?

You could review the following notes which may help you debug this more

Will update this answer if I find other resources to resolve this.

Related