Running cypress container with docker and custom network

87 Views Asked by At

I have this code in my gitlab-ci.yml:

stages:
  - build
  - test

build_frontend:
  image: node:18.13.0
  stage: build
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - dist/web
  only:
    - feature/gitlab-cy

run_cypress_tests:
  image: cypress/base:18.16.0
  services:
    - name: docker:20.10-dind
      alias: docker
  stage: test
  variables:
    DOCKER_HOST: tcp://docker:2375
  before_script:
    - apt-get update && apt-get install -y docker.io curl
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN $CI_REGISTRY
    - export PATH="$(npm bin):$PATH"
    - docker network create custom_network || true # Create a custom network
    - docker run -d --name mongodb --network custom_network -e MONGO_INITDB_ROOT_USERNAME=... -e MONGO_INITDB_ROOT_PASSWORD=... mongo:latest

  script:
    - docker pull registry.gitlab.com/mistigri/kfait/services:latest
    # Run the backend service on custom_network
    - docker run -d -p 3000:3000 --name backend-service --network custom_network -e [LIST OF EN VARS] registry.gitlab.com/mistigri/kfait/services:latest
    - sleep 120

    - docker logs backend-service
    # Serve the frontend using http-server on custom_network
    - docker run -d --name frontend-service --network custom_network -v $(pwd)/dist/web:/web -p 4200:4200 node:18.13.0 sh -c "npm install -g http-server && http-server /web -p 4200 -c-1"
    - sleep 60

    - docker logs frontend-service

    - docker run --name cypress --network custom_network -v $(pwd):/e2e -w /e2e cypress/base:18.16.0 /bin/bash -c "curl http://frontend-service:4200 && npm install && npx cypress run --config baseUrl=http://frontend-service:4200 --env apiUrl=http://backend-service:3000,frontUrl=http://frontend-service:4200"

    - docker network inspect custom_network
    - docker ps
  dependencies:
    - build_frontend
  only:
    - feature/gitlab-cy

And i have always this error in my gitlab pipeline: CypressError: cy.visit() failed trying to load: http://frontend-service:4200/auth/login or all others url that exist, and are available on localhost testings.

Cypress is unable to reach the frontend-service, that is running in the same custom_network as the Cypress container (custom_network).

I tried to curl my both services frontend-service and backend-service and both are responding well. My variables in my --config of cypress docker run are well (--config baseUrl=http://frontend-service:4200 --env apiUrl=http://backend-service:3000,frontUrl=http://frontend-service:4200).

I can not curl inside of Cypress container, because the test are failing, so i am not able to launch - docker network inspect custom_network to see if cypress is well running in the same custom_network, and present in the containers object.

I know that the frontend-service is avaiblable, because he is in the containers object of the custom_network, and the logs are ok:

Starting up http-server, serving /web
Available on:
  http://127.0.0.1:4200
  http://172.19.0.4:4200

Does someone already used Cypress in CI CD, and now why it's impossible to make it connected with the service, instead of launching a http-server directly on CI CD gitlab, that make and IP available on docker network.

Thanks for your help.

0

There are 0 best solutions below