Cypress Docker container does not connect to running server
23:13 27 Dec 2022

[ EDIT: I am not deleting the question even if it could be a duplicate of this one, because the original question might be harder to search. In case this were not advisable, please feel free to delete/close. ]

I have this docker-compose:

x-common-postgres-env:
  &common-postgres-env
    POSTGRES_DB: ${POSTGRES_DB}
    POSTGRES_USER: ${POSTGRES_USER}
    POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    POSTGRES_PORT: 5432

x-common-postgres:
  &common-postgres
  image: postgres:13.4
  hostname: postgres
  environment:
    << : *common-postgres-env
  ports:
    - "5432:5432"
  healthcheck:
    test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"]

x-common-django:
  &common-django
  build: .
  environment:
    &common-django-env
    << : *common-postgres-env
    DJANGO_SECRET: ${DJANGO_SECRET}
    ALLOWED_HOSTS: ".localhost 127.0.0.1 [::1]"
    CORS_ALLOWED_ORIGINS: "http://localhost:8000"
    CSRF_TRUSTED_ORIGINS: "http://localhost:8000"
  healthcheck:
    test: ["CMD", "wget", "-qO", "/dev/null", "http://localhost:8000"]
  ports:
    - "8000:8000"


services:

  db:
    << : *common-postgres
    profiles:
      - prod
    volumes:
      - ./data/db:/var/lib/postgresql/data

  db-test:
    << : *common-postgres
    profiles:
      - test


  web:
    << : *common-django
    profiles:
      - prod
    command: pdm run python manage.py runserver 0.0.0.0:8000
    environment:
      << : *common-django-env
      POSTGRES_HOST: db
    volumes:
      - ./KJ_import:/code/KJ_import
      - ./docs:/code/docs
      - ./KJ-JS:/code/KJ-JS
      - ./static:/code/static
      - ./media:/code/media
      - ./templates:/code/templates
    depends_on:
      db:
        condition: service_healthy

  web-test:
    << : *common-django
    profiles:
      - test
    command: pdm run python manage.py runserver 0.0.0.0:8000
    environment:
      << : *common-django-env
      POSTGRES_HOST: db-test
    depends_on:
      db-test:
        condition: service_healthy

  cypress:
    # image: "cypress/included:9.2.0"
    profiles:
      - test
    build:
      context: .
      dockerfile: Dockerfile.cy
    # command: ["--browser", "chrome"]
    environment:
      CYPRESS_baseUrl: http://localhost:8000/
    working_dir: /code/KJ-JS
    volumes:
      - ./KJ-JS:/code/KJ-JS
      - ./media:/code/media
    depends_on:
      web-test:
        condition: service_healthy

This Dockerfile.cy

FROM cypress/included:9.2.0

# WORKDIR /code/KJ-JS
COPY system.conf /etc/dbus-1/system.conf
RUN chmod 644 /etc/dbus-1/system.conf

COPY entrypoint.cy.sh /
ENTRYPOINT ["/bin/sh", "/entrypoint.cy.sh"]

and this entrypoint.cy.sh to activate the Cypress tests:

#!/bin/sh

echo "### Create DBus"

dbus-uuidgen > /var/lib/dbus/machine-id

mkdir -p /var/run/dbus
dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address &

# Wait for the D-Bus system bus address to be available
while [ -f /var/run/dbus/system_bus_socket ]; do
   sleep 1
done

# Check if the dbus-daemon process is running
if ps -ef | grep -v grep | grep dbus-daemon > /dev/null; then
   echo "### D-Bus daemon is running"
else
   echo "### D-Bus daemon is not running"
fi

# Check if the D-Bus configuration files are correctly configured
if [ -f /etc/dbus-1/system.conf ]; then
   echo "### D-Bus system configuration file is present"
else
   echo "### D-Bus system configuration file is missing"
fi

# Make sure that the /var/run/dbus directory exists and is writable by the dbus-daemon process
if [ -d /var/run/dbus ]; then
   if [ -w /var/run/dbus ]; then
      echo "### /var/run/dbus is writable by the dbus-daemon process"
   else
      echo "### /var/run/dbus is not writable by the dbus-daemon process"
   fi
else
   echo "### /var/run/dbus does not exist"
fi

# Remove the /var/run/dbus/pid file if it exists
if [ -f /var/run/dbus/pid ]; then
   rm -f /var/run/dbus/pid
   echo "### /var/run/dbus/pid file removed"
else
   echo "### /var/run/dbus/pid file does not exist"
fi

echo "### Bus active"

cd /code/KJ-JS
cypress run --headed --browser chrome
echo "### after cypress run"

exec "$@"

When I run the docker compose --profile test up, the db spins well, django gets up and running, but Cypress cannot seem to connect.

It complained of not having Dbus running, so I added it in the entrypoint shown above, and tested all of its components, yet the error message still comes up:

kj_import-web-test-1  | System check identified no issues (0 silenced).
kj_import-web-test-1  | December 28, 2022 - 02:32:40
kj_import-web-test-1  | Django version 2.2.28, using settings 'KJ_import.settings'
kj_import-web-test-1  | Starting development server at http://0.0.0.0:8000/
kj_import-web-test-1  | Quit the server with CONTROL-C.
kj_import-web-test-1  | [28/Dec/2022 02:32:42] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:32:42] "GET /static/favicon.ico HTTP/1.1" 200 9662
kj_import-web-test-1  | [28/Dec/2022 02:32:46] "GET /docs/register/ HTTP/1.1" 200 6551
kj_import-web-test-1  | [28/Dec/2022 02:32:49] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:33:02] "GET / HTTP/1.1" 200 5776
kj_import-cypress-1   | ### Create DBus
kj_import-cypress-1   | ### D-Bus daemon is running
kj_import-cypress-1   | ### D-Bus system configuration file is present
kj_import-cypress-1   | ### /var/run/dbus is writable by the dbus-daemon process
kj_import-cypress-1   | ### /var/run/dbus/pid file does not exist
kj_import-cypress-1   | ### Bus active
kj_import-cypress-1   | unix:path=/var/run/dbus/system_bus_socket,guid=1181acd37ea51796e63af6a863ab9ccf
kj_import-cypress-1   | [26:1228/013304.773071:ERROR:bus.cc(392)] Failed to connect to the bus: Address does not contain a colon
kj_import-cypress-1   | [26:1228/013304.773122:ERROR:bus.cc(392)] Failed to connect to the bus: Address does not contain a colon
kj_import-cypress-1   | [213:1228/013304.794142:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
kj_import-cypress-1   | Cypress could not verify that this server is running:
kj_import-cypress-1   | 
kj_import-cypress-1   |   > http://localhost:8000/
kj_import-cypress-1   | 
kj_import-cypress-1   | We are verifying this server because it has been configured as your `baseUrl`.
kj_import-cypress-1   | 
kj_import-cypress-1   | Cypress automatically waits until your server is accessible before running tests.
kj_import-cypress-1   | 
kj_import-cypress-1   | We will try connecting to it 3 more times...
kj_import-cypress-1   | We will try connecting to it 2 more times...
kj_import-cypress-1   | We will try connecting to it 1 more time...
kj_import-cypress-1   | 
kj_import-cypress-1   | Cypress failed to verify that your server is running.
kj_import-cypress-1   | 
kj_import-cypress-1   | Please start this server and then run Cypress again.
kj_import-cypress-1   | ### after cypress run
kj_import-cypress-1 exited with code 0
kj_import-web-test-1  | [28/Dec/2022 02:33:32] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:34:02] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:34:32] "GET / HTTP/1.1" 200 5776

Please note that the server is running fine. You can see it from the above log (GETs replied with 200, even before the Cypress container starts trying to connect) and I can access it from my local browser.

What am I missing here?

Thanks in advance!

docker docker-compose cypress sh