Monk CIMonk CI home

22 Nov, 2026Aishwarya Palta

Reliable Container Integration Tests in CI


Make container integration tests reliable in GitHub Actions with PostgreSQL health checks, dynamic ports, isolated data and cleanup after each CI job.

Reliable container integration tests need a ready service, the right network address and disposable data. A container marked “running” can still reject connections while its database initializes.

Our developer rule: make readiness explicit before adding retries. Otherwise, retries can hide a startup race and waste time on every pull request.

How do you wait for PostgreSQL?

Use a health check instead of a fixed sleep. Add this service configuration to an existing Linux GitHub Actions job with Docker available:

services:
  postgres:
    image: postgres:17
    env:
      POSTGRES_USER: ci
      POSTGRES_PASSWORD: ci
      POSTGRES_DB: ci
    ports:
      - 5432/tcp
    options: >-
      --health-cmd "pg_isready -U ci -d ci"
      --health-interval 5s
      --health-timeout 5s
      --health-retries 12

These credentials are disposable test fixtures. Select an image version matching your application, and pin a reviewed image digest when exact reproducibility matters. The intervals are starting settings to tune, not measured recommendations.

A successful readiness probe means PostgreSQL accepts connections; run your schema migrations before application tests. GitHub’s PostgreSQL example demonstrates this service pattern.

Which address should tests use?

For tests running directly on the runner, pass the assigned host port to your existing test command:

- name: Integration tests
  env:
    DATABASE_URL: postgresql://ci:ci@127.0.0.1:${{ job.services.postgres.ports[5432] }}/ci
  run: npm run test:integration

This assumes checkout, Node setup, dependency installation and a script reading DATABASE_URL already exist. Our Node.js workflow covers that preparation.

If the job itself runs inside a container, use the service hostname postgres and port 5432 instead. GitHub documents both networking modes and service cleanup.

What should happen after failure?

GitHub destroys job service containers when the job completes. Avoid persistent host mounts for test databases, and give parallel test workers separate schemas or databases to prevent fixture collisions.

Preserve useful test reports before cleanup, excluding credentials. A teardown failure deserves investigation rather than a green retry.

For platform checks, see our acceptance-testing approach. On Monk CI, follow the runner setup and keep the same readiness contract.

Takeaway: test application behavior after proving the environment is ready.

Try Monk CI for free

Continue with GitHub