QA Interview Hub · Knowledge base · Quizzes · Українська

CI/CD › Docker for automated tests

docker compose for e2e: app + db + tests

Docker Compose starts the app, database and test runner with one command in an ephemeral environment per run, so parallel PRs do not disturb each other. Services reach each other by service name on the Compose network, not through localhost. Readiness comes from a healthcheck with condition: service_healthy, and --exit-code-from passes the tests' exit code to CI.

CI/CD · Docker

Tests start before the database and fail with "connection refused", yet CI is green. How is that possible?

E2E tests hit a shared stage: it holds other people's data, and parallel PRs spoil each other's state. The team starts its own environment with docker compose and gets two problems at once: tests start before the database accepts connections, and the pipeline returns success even though tests failed. Both come from Compose not knowing, by default, about service readiness and the tests' exit code.

How it works and why. Compose starts the app, the database and the test runner with one command and then cleans everything up. The idea is an ephemeral environment for every run: an empty DB, a known state, no shared data, so parallel PRs do not disturb each other. Services see each other by service name (db, app) in a shared Compose network, not through localhost.

What depends_on means

Wrong

depends_on without condition

Guarantees only container start order. The DB process is still initializing while the tests already connect.

Right

healthcheck + service_healthy

The dependent service waits until the health check succeeds, meaning the database really accepts connections.

Figure 1. A started container does not mean a ready service.

Start order

dbhealthcheck: pg_isreadyappwaits for db: service_healthye2ewaits for app: service_healthy, then tests
Figure 2. Every dependency arrow rests on health, not on start.

Example. A compose.yaml file with three services. The version key is no longer needed:

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: test
      POSTGRES_DB: app_test
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres -d app_test']
      interval: 2s
      timeout: 3s
      retries: 15

  app:
    build: .
    environment:
      DATABASE_URL: postgres://postgres:test@db:5432/app_test
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ['CMD-SHELL', 'curl -fs http://localhost:3000/health || exit 1']
      interval: 2s
      timeout: 3s
      retries: 30

  e2e:
    image: mcr.microsoft.com/playwright:v1.65.1-noble
    working_dir: /work
    volumes:
      - ./:/work
    environment:
      BASE_URL: http://app:3000
    depends_on:
      app:
        condition: service_healthy
    command: sh -c "npm ci && npx playwright test"
# everything starts, tests run, the exit code comes from e2e
docker compose up --build --abort-on-container-exit --exit-code-from e2e

# remove containers, the network and volumes
docker compose down -v

What matters here:

  • --exit-code-from e2e returns the tests' exit code to CI. Without it up may finish with success although tests failed; --exit-code-from already implies --abort-on-container-exit, so the other services stop when the tests end.
  • Secrets and config go through environment or env_file (from secrets in CI), not baked into an image.
  • Reports reach the host through the volume ./:/work: the playwright-report/ folder is available to the next CI step for upload-artifact.
  • down -v removes volumes so the next run starts with a clean DB.

Limits and alternatives. For the app's healthcheck the image needs curl or an equivalent: minimal images lack it, and the service stays "unhealthy" forever. The test runner can be started separately: docker compose up -d --wait starts services and waits for health, and docker compose run --rm e2e runs the tests. If only a database is needed, a service container in GitHub Actions is simpler (the lesson “Secrets, service containers, GitLab CI and Jenkins”). The Playwright image version follows the rule from the lesson “Playwright in the official Docker image”.

Typical mistakes.

  • Using localhost instead of the service name.
  • depends_on without condition: service_healthy.
  • No --exit-code-from: a green pipeline with failed tests.
  • A healthcheck with a command missing from the image.
  • Forgetting down -v: an old database spoils the next run.

Check yourself

A pipeline with docker compose up is green although the logs show failed tests. What to add?

Answer

The flags --abort-on-container-exit and --exit-code-from e2e: then the command ends with the e2e service's code and CI sees the failure.

Why does the e2e container reach the app at http://app:3000 and not http://localhost:3000?

Answer

localhost inside the e2e container points to that very container. Services see each other by name in the Compose network, so the address is app:3000.

In an interview. Junior: Docker Compose starts the app, the database and the tests with one command, and services reach each other by name. Middle/Senior: The environment is ephemeral per run, so parallel PRs do not disturb each other. I ensure readiness with a healthcheck and condition: service_healthy, because a bare depends_on guarantees only start order. I pass the tests' exit code to CI through --exit-code-from, bring reports to the host through a volume, and run down -v after the run.

More articles in this chapter