Modern QA2026Docker Compose for Test Environments — tiles
Log inJoin
45 / 55 · 19 Linux & Command Line · Docker Basics for QA← prev⊞ allnext →☰ Read as one page

6.5Docker Compose for Test Environments

Docker Compose lets you define multi-container environments in a YAML file. This is the standard way to manage test environments that require multiple services.

Example: docker-compose.test.yml

version: '3.8'

services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: test
      DATABASE_URL: postgres://postgres:testpass@db:5432/test_db
      REDIS_URL: redis://cache:6379
      MAIL_HOST: mail
      MAIL_PORT: 1025
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started

  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: testpass
      POSTGRES_DB: test_db
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

  cache:
    image: redis:7
    ports:
      - "6379:6379"

  mail:
    image: axllent/mailpit
    ports:
      - "1025:1025"
      - "8025:8025"

Compose Commands

# Start all services
docker compose -f docker-compose.test.yml up -d

# View logs from all services
docker compose -f docker-compose.test.yml logs -f

# View logs from a specific service
docker compose -f docker-compose.test.yml logs -f app

# Run tests against the environment
npm run test:integration

# Stop and remove everything (including volumes)
docker compose -f docker-compose.test.yml down -v

# Rebuild after code changes
docker compose -f docker-compose.test.yml up -d --build

Full Test Cycle with Compose

# One-command test cycle
docker compose -f docker-compose.test.yml up -d && \
  npm run test:integration ; \
  docker compose -f docker-compose.test.yml down -v

The semicolon (;) before down ensures cleanup happens even if tests fail.