A job must run with a specific Docker image and connect to a service container.

BeginnerScenarioGitLab CI
Answer

Situation read → Services in GitLab.

What I'd do →

test:
  image: python:3.12
  services:
    - name: postgres:16
      alias: db
  variables:
    POSTGRES_PASSWORD: test
    DATABASE_URL: postgresql://postgres:test@db:5432/postgres
  script: pytest

Why → Services are reachable via their alias hostname on the job network.

Interview tip → Add a readiness wait; the service may not be accepting connections when the script starts.

All CI/CD questions Roadmap: GitLab CI/CD

More on GitLab CI