How do I spin up a full test environment with docker-compose?
Need app + Postgres + Selenium Grid for E2E on CI. What does a clean compose look like?
dockerdocker-composee2e
Need app + Postgres + Selenium Grid for E2E on CI. What does a clean compose look like?
Accepted solution
Docker Compose gives every test run the same disposable stack: app, database, and any stubs, on an isolated network.
services:
db:
image: postgres:16
environment: { POSTGRES_PASSWORD: test }
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 3s
retries: 20
app:
build: .
depends_on:
db: { condition: service_healthy }
ports: ["8080:8080"]
tests:
image: mcr.microsoft.com/playwright:v1.50.0-jammy
depends_on: [app]
environment: { BASE_URL: "http://app:8080" }
command: npx playwright testThe two details that stop flakes: use real `healthcheck` + `depends_on: condition: service_healthy` instead of `sleep 10`, and address services by their compose name (`http://app:8080`) rather than localhost. Run CI with `docker compose up --abort-on-container-exit --exit-code-from tests`, then `docker compose down -v` so no volume state leaks into the next run.