The strongest argument for Testcontainers is simple: stop mocking your database. In-memory SQLite fakes and stubbed repositories happily pass tests right up until a real PostgreSQL migration, lock contention race, or Redis command fails in production.
We agree completely with that premise—and yet we do not use Testcontainers anywhere in vitruvian-core.
Our backend services, Vitruvian Tabs and Vitruvian Relay, still test against real PostgreSQL 16 and real Redis on every test run. Instead of asking a host Docker daemon to pull and boot containers, Bazel launches SHA-256-pinned postgres and source-built redis-server binaries directly inside the test sandbox through rules_itest.
Figure 1: Real-database integration tests work best when the database behaves like a deterministic build artifact inside the test sandbox rather than an external daemon dependency.
1. Why a Host Docker Socket Fights a Bazel Monorepo
Testcontainers works well in Maven, Gradle, or Go test suites that already assume the test runner has unrestricted access to the host machine and /var/run/docker.sock. Inside a Bazel monorepo, punching through the sandbox to talk to an external daemon creates three problems:
- Broken hermeticity and caching: A test that dials
/var/run/docker.sockdepends on whatever Docker daemon, cached image tags, and network proxy exist outside Bazel’s input graph. - A mandatory local daemon everywhere: Every developer laptop and CI worker has to run Docker (plus container cleanup via Ryuk) just to run unit-adjacent store tests.
- Slower startup for simple single-binary services: PostgreSQL and Redis are standalone native binaries. Wrapping a single process in an OCI image, a container runtime, and a virtual bridge network adds complexity when you already have a build system that pins binaries by checksum.
In vitruvian-core, MODULE.bazel declares rules_itest (0.0.56) alongside two hermetic service toolchains:
- PostgreSQL
16.13.0(theseus-rs/postgresql-binaries): Pinned viahttp_archivewith SHA-256 digests for all four platforms (linux_amd64,linux_arm64,darwin_arm64,darwin_amd64), matching production PG 16. On Linux we specifically select thegnubuilds rather thanmuslbecause our CI and remote build images useglibc. - Redis (
@redisfrom BCR): Compiled directly from source as acc_binary(@redis//:redis-serverand@redis//:redis-cli), with zero external downloads at test time.
Figure 2: Both approaches test against real database engines. The difference is the boundary: Testcontainers calls out to a host Docker socket, while
rules_itest keeps the database binary, temp directory, and lifecycle inside Bazel’s test graph.
2. How a Test Boots a Real Database in Milliseconds
When a service_test runs in Vitruvian Tabs or Vitruvian Relay, Bazel builds the service launcher and passes it to rules_itest’s svcinit harness, which assigns an unused loopback port via ASSIGNED_PORTS and creates an isolated $TEST_TMPDIR.
Our Postgres launcher (tools/testinfra/postgres/src/lib.rs) runs initdb into $TEST_TMPDIR/pgdata and execs postgres directly on 127.0.0.1 with durability turned off for throwaway test speed:
initdbflags:--username=postgres --auth=trust --no-syncpostgresflags:-c listen_addresses=127.0.0.1 -c unix_socket_directories= -c fsync=off -c synchronous_commit=off -c full_page_writes=off- Readiness gate:
check_postgresrunspg_isready -h 127.0.0.1 -p <port> -U postgresuntil the server accepts connections.
From there, the two apps layer their own workflows on top of the same itest_service pattern:
- Chained migrations and API tests (
Vitruvian Tabs): Inapps/suites/vitruvian-tabs/api/BUILD,:migrateis anitest_taskthat depends on:postgresand runs our Rust migration wrapper (migrate_deploy_bin) with a vendored, SHA-256-pinned Prismaschema-enginebinary.:migrate_deploy_itestthen queries_prisma_migrationsagainst the migration directories on disk to prove every migration actually applied. The full:api_servicewaits on both:migrateand:redis. - Per-test logical databases (
Vitruvian Relay): Instead of booting a new Postgres process per test case,apps/suites/vitruvian-relay/relay/tests/pg_support.rskeeps one sharedpostgresservice per test target and callsfresh_database()(CREATE DATABASE t_<random>) in each#[tokio::test]so tests run concurrently without seeing each other’s rows. - Loopback enforcement (
Redis): Everyredis-serverstarts with--bind 127.0.0.1, and//tools/testinfra/redis:check_loopbackinspects/proc/net/tcp{,6}on Linux and/usr/sbin/lsofon macOS to fail the build if a test service ever opens a wildcard listener.
3. Four Real Bugs That Bit Us
Dropping Docker does not eliminate systems problems—it moves them directly into your process lifecycle. Four real bugs forced us to harden the setup:
Figure 3: Four concrete catches we hit in
vitruvian-core: macOS fork() locale threads, a 0.75-second port reservation race, root UID restrictions under remote execution, and mid-test outage simulation.
- macOS
postmastercrashes whenfork()meets CoreFoundation: On macOS, calling system locale APIs beforefork()initializes CoreFoundation background threads, causing PostgreSQL to abort immediately with"postmaster became multithreaded during startup". ForcingLC_ALL=CandLANG=Con bothinitdbandpostgresinrun_postgres.rseliminates the multithreaded pre-fork state. - The 0.75-second
svcinitport race (#389):rules_itest’ssvcinitreservesautoassign_port = Trueby binding127.0.0.1:0, recording the port, and immediately closing the socket. Becauseinitdbtakes roughly0.75sbeforepostgresbinds that port, two local tests running concurrently under macOSprocesswrapper-sandbox(which shares the host network namespace) occasionally collided on the exact same port. Tagging local Postgres targets"exclusive-if-local"serializes uncached local Postgres runs while leaving cached targets instant. - Remote execution containers run as
root: BuildBuddy remote execution (RBE) runs actions inside Linux containers as UID0. Source-builtredis-serverhandles that fine—soredis_loopback_testruns remotely on RBE—but PostgreSQL refuses to run asrootby design. Everypostgrestarget and dependent test carries"no-remote-exec"so it executes on the CI runner’s regular non-root user instead. - Testing database outages without stopping a container:
rules_iteststarts a service for the lifetime of a test binary and cannot pause or killpostgresmid-test. To verify reconnect and backoff behavior inapps/suites/vitruvian-relay/relay/tests/database_down.rs,pg_support::Proxyinserts a tiny Tokio TCP proxy betweenPgStoreand Postgres with four switchable modes:Open,Down(drops connections immediately),Silent(holds sockets open without answering), andSlow(Duration)(injects latency). That simulates a dead or degraded database deterministically in milliseconds.
4. Where Docker and Testcontainers Still Win
Skipping Testcontainers is not dogma; it is a trade-off based on what we build:
- Most of our monorepo never talks to a server database: The majority of
vitruvian-coreconsists of native macOS/Android apps, CLIs, MCP servers, ESP32-S3 firmware, and local SQLite stores. Only two application suites need PostgreSQL or Redis. - When we do test a full container image, we use Docker on purpose:
//tools/ci:backstage_boot_smokebuilds our Backstage OCI image, starts a throwawaypostgrescontainer and Docker network, and verifies every backend plugin initializes toREADY. The unit under test there is the assembled container image itself, not a store library. - When Testcontainers makes sense: If your stack depends on multi-process or JVM-heavy services that are painful to vendor as standalone binaries—Kafka, Elasticsearch, Keycloak, or LocalStack—Testcontainers earns its Docker dependency immediately.
Key Takeaways
- Test against real engines, not mocks—but question the container tax. If your dependencies are single binaries like PostgreSQL and Redis, running pinned executables inside Bazel’s sandbox gives you real query planners and wire protocols with zero daemon setup.
- Expect OS and sandbox edge cases at the process boundary. Locale threads on macOS, port-reservation TOCTOU windows, and UID
0restrictions in remote builders show up quickly once you own process startup directly. - Keep an escape hatch for container-shaped tests. Reserve Docker for verifying final OCI images or heavy external stacks rather than making a host daemon the price of admission for every
bazel test.
You can inspect the hermetic database launchers under tools/testinfra and see both integration patterns in apps/suites/vitruvian-tabs/api and apps/suites/vitruvian-relay/relay.