Gitlab CI with Docker images – Flask microservice testing database

I am trying to deploy a Flack app/service which is built into a docker container to Gitlab CI. I am able to get everything working via docker-compose except when I try to run tests against the postgres database I am getting the below error:

Is the server running on host “events_db” (172.19.0.2) and accepting
TCP/IP connections on port 5432?

Presumably this is because the containers can’t see each other. I’ve tried many different methods. But below is my latest. I have attempted to have docker-compose spin up both containers (just like it does on local), run the postgres db as a git lab service, run from a python image instead of a docker image, use a docker.prod.yml where I remove the volumes and variables.

Nothing is working. I’ve checked just about every link that shows up on google when you look for ‘gitlab ci docker flask postgres’ and I believe that I am massively misunderstanding the implementation.

I do have gitlab runner up and going.

.gitlab-ci.yml

image: docker:latest
services:
  - docker:dind
  - postgres:latest

stages:
  - test

variables:
  POSTGRES_DB: events_test
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres
  DATABASE_URL: postgres://[email protected]:5432/events_test
  FLASK_ENV: development
  APP_SETTINGS: app.config.TestingConfig
  DOCKER_COMPOSE_VERSION: 1.23.2

before_script:
  #- rm /usr/local/bin/docker-compose
  - apk add --no-cache py-pip python-dev libffi-dev openssl-dev gcc libc-dev make
  - pip install docker-compose

  #- mv docker-compose /usr/local/bin
  - docker-compose up -d --build

test:
  stage: test
  #coverage: '/TOTAL.+ ([0-9]{1,3}%)/'

  script:
    - docker-compose exec -T events python manage.py test

after_script:
  - docker-compose down

docker-compose.yml

version: '3.3'

services:
  events:
    build:
      context: ./services/events
      dockerfile: Dockerfile

    volumes:
      - './services/events:/usr/src/app'

    ports:
      - 5001:5000

    environment:
      - FLASK_ENV=development
      - APP_SETTINGS=app.config.DevelopmentConfig
      - DATABASE_URL=postgres://postgres:[email protected]_db:5432/events_dev  # new
      - DATABASE_TEST_URL=postgres://postgres:[email protected]_db:5432/events_test  # new

  events_db:
    build:
      context: ./services/events/app/db
      dockerfile: Dockerfile

    ports:
      - 5435:5432

    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

Source: StackOverflow