How to run tests stage to test running containers in Gitlab pipeline

my gitlab-ci.yml is:

services:
- docker:dind

before_script:
  - docker info
  - apk update
  - apk upgrade
  - apk add curl jq python3 python3-dev build-base libffi-dev libressl-dev gettext
  - curl -O https://bootstrap.pypa.io/get-pip.py
  - python3 get-pip.py
  - pip install docker-compose
  - pip install pytest


build:
  stage: build
  script:
        -  docker image prune -f
        -  docker-compose -f docker-compose.yml build --no-cache
        -  docker-compose -f docker-compose.yml up -d




test:
    stage: test
    variables:
      DOCKER_TLS_CERTDIR: ""
    script:
        - docker ps 
        - pip install -r requirements.txt
        - pip install pytest-docker-compose
        - pwd
        - ls -l
        - export PYTHONPATH="$PYTHONPATH:."
        - python3 -c "import sys;print(sys.path)"
        - docker-compose -f docker-compose.yml build --no-cache
        - docker-compose -f docker-compose.yml up -d
        - docker ps

        - pytest --use-running-containers
        - apk install redis-cli
        - pytest --docker-compose-no-build --use-running-containers
   

     - pytest --docker-compose=docker-compose.yml

And a dockercompose.yml

version: '3.4'
networks:
  internal_network:
services:
  web:
    build: ./HTTPSERV/
    ports:
      - 8080:8080
    networks: 
      - internal_network
    volumes:
      - ~/message-broker:/data/app
  rabbit:
    image: rabbitmq:3-management
    networks:
      - internal_network
    ports: 
      - 15672:15672
  obse:
    build: ./OBSE/
    networks: 
      - internal_network
    depends_on: 
      - rabbit
    volumes:
      - ~/message-broker:/data/app
    restart: always
  imed:
    build: ./IMED/
    networks: 
      - internal_network
    depends_on: 
      - rabbit
      - obse
    restart: always
  orig:
    build: ./ORIG/
    networks: 
      - internal_network
    restart: always
    depends_on: 
      - web
      - rabbit
      - obse
      - imed
  redis:
    image: redis
    networks:
      - internal_network
    depends_on: 
      - web
    ports:
      - 6379:6379 

Gitlab pipeline gives error:

redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused.

I have tried using pytest-docker-compose and other solutions but nothing is helping Where is the error and how can this be solved The first stage is the build and it succceeds, even inspected the containers during the tests and they seem running