Creating test environments with Docker Compose
Creating test environments with Docker Compose gives Agile teams a repeatable way to run APIs, databases, queues and supporting tools on one developer laptop or in a CI/CD pipeline. A versioned YAML file defines the services, so every engineer can start from the same known state instead of relying on undocumented setup steps.
The approach is especially useful for integration and end-to-end testing, where dependencies often drift across team members’ machines. It also gives DevOps teams a practical bridge between local development and automated continuous testing, while keeping production-like behaviour explicit, portable and easy to review.
Choose the right level of isolation
Start by deciding how much isolation the scenario needs. A single service may only require a local runtime, whereas an end-to-end suite can benefit from separate containers, an internal network and disposable data. The comparison below keeps the decision proportional rather than turning every test into an infrastructure project.
| Approach | Setup effort | Isolation | Typical use | Main limitation |
|---|---|---|---|---|
| Native installation | Low initially | Low | One-off scripts and small tools | Dependency conflicts |
| Docker Compose | Medium | Medium | Integration and end-to-end tests | Host resource limits |
| Virtual machine | High | High | Legacy services needing a full OS | Slow and costly to maintain |
| Cloud sandbox | Medium | High | Shared pre-release validation | Running cost and access controls |
Docker Compose sits between ad hoc local installs and a managed cloud sandbox. It offers stronger dependency control with less operational overhead than a virtual machine, while remaining easier to launch for product teams. For production-scale load or highly available failover, use Kubernetes or a managed platform; for feature-level integration, a Compose stack is usually the sensible balance.
Define services, data and dependencies
Map each test dependency before writing YAML: the API under test, database engine, cache, broker and any mock of a third-party system. Separate persistent business data from ephemeral test fixtures. Give each service a stable hostname, because service discovery through the Compose network removes hard-coded IP addresses and makes the stack portable between a laptop and an Australian cloud region.
Choose images deliberately. Pin application releases and infrastructure versions, avoid floating tags such as latest, and record whether a service is built locally or pulled from a registry. For regulated data, nFocus’s regulated test data strategies explain how synthetic or masked records can preserve realistic relationships without exposing personal information. Store the generation rules with the project so every run recreates the same baseline.
Build the Compose configuration
Keep the configuration in source control beside the application and review changes like code. A compact file can define the complete dependency graph while remaining readable by product teams. A simplified API and PostgreSQL stack might look like this:
services:
api:
image: registry.example.au/payments-api:2.8.1
ports:
- "127.0.0.1:8080:8080"
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD:?Set DB_PASSWORD}
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
volumes:
pgdata:
Start with docker compose up -d --build --wait, then use docker compose ps and docker compose logs to confirm that every service is healthy. The --wait option prevents a test run from racing database initialisation, while a separate application health check can confirm that the API is ready to accept requests. Store development passwords in an ignored .env file or injected CI variables, never in version control.
Use Compose profiles to run optional tools, such as a mail catcher or trace viewer, without starting them for every suite. Override files can vary a browser, database image or feature flag between local development and CI. Keep overrides small and intention-based; if the differences multiply, the team may need two clearly named environment specifications instead of a fragile patchwork.
Connect networks, volumes and secrets
Services on the same Compose network reach one another by service name, while published ports expose only what a developer needs. Binding to 127.0.0.1 protects a local database from casual access across an office network, which matters for engineers working from shared spaces or connecting through an Australian corporate VPN. Define explicit networks when containers need separation, and give parallel CI jobs distinct project names so networks, volumes and container names remain isolated. On a shared runner, use dynamic published ports or separate runners rather than assigning the same host port to concurrent stacks.
Treat secrets as sensitive inputs. Compose can mount secret files or inject environment variables, but that mechanism does not replace proper access controls, rotation or encrypted storage in a managed platform. Never bake credentials into an image, because every layer and cached build may retain them. When a service accepts uploads or returns downloadable reports, secure file workflow testing helps teams verify authorisation, malware scanning, content-type checks and clean-up of temporary files.
Keep test data controlled and repeatable
Make initialisation explicit with schema migrations, seed scripts and small deterministic fixtures. Give test records predictable identifiers, reset state between suites and avoid depending on the order in which parallel tests run. Container images should contain application code and configuration, while disposable volumes hold changing data. This separation allows a clean environment to be created in minutes without rebuilding every image.
For CI/CD, create an isolated stack for each branch or pull request and remove it when the job ends. A teardown such as docker compose down -v --remove-orphans clears volumes and stray containers, but it should be paired with a clear ownership boundary to prevent the wrong project data from being deleted. Cache dependency layers and test images, not database state, so speed improves without carrying yesterday’s failures into today’s run.
Australian businesses subject to the Privacy Act 1988 must handle personal information in line with the Australian Privacy Principles, and the Notifiable Data Breaches scheme can require notification after a serious compromise. Keep production customer records out of ordinary development stacks unless the purpose, access and retention are lawfully justified. Prefer synthetic or appropriately de-identified data, restrict it to named roles and record how test environments are created, accessed and destroyed.
Run the stack across Australian delivery settings
Many Australian organisations deliver through Microsoft-centric estates that include Azure DevOps, Windows-hosted applications and Microsoft tooling. Docker Desktop with Windows Subsystem for Linux is a common developer setup in this market, while Linux containers can still run the application, database and supporting services consistently. Test automation should therefore work in both the team’s normal desktop environment and a hosted agent without relying on Windows-only executables inside the container.
Teams in Sydney and Melbourne can use established cloud regions from Microsoft, Amazon Web Services and Google Cloud, while colleagues in Perth or Brisbane may connect across greater distance. Place stateful test services in an Australian region when data residency, latency or contractual controls require it, and keep the same container images across environments. Run load tests separately because a Compose stack on one laptop cannot represent distributed production traffic.
Distributed teams in Brisbane and Perth often depend on shared CI runners, so the environment specification must account for network latency and Australian daylight-saving changes. Scheduled jobs, token expiry and time-sensitive tests should use UTC, while assertions retain business-local expectations. For government and regulated customers, align patching, access controls and evidence with the Australian Signals Directorate’s Essential Eight. It is guidance rather than legislation, but it is frequently used as a security baseline in Australian procurement.
Make the workflow a team habit
Treat the Compose definition as a maintained product artefact. Give it an owner, review image and dependency changes, and release it with the application it supports. Include environment creation, seeded data, health checks and teardown in the CI/CD pipeline so continuous testing measures the application rather than an engineer’s setup. Track startup time, failed-job rate and flaky suites to identify where the environment needs attention.
A nFocus software testing consultancy can assess an existing delivery pipeline, establish a reference stack and train the team in test automation, Agile and DevOps practices. Choose one representative user journey, recreate its dependencies and measure the result before expanding the pattern. Book a Docker Compose environment workshop with nFocus and leave the session with a working stack, a pipeline and a documented reset process.