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.

Isometric Vitruvian blueprint illustration of a sealed sandbox housing a database core and cache module linked by loopback traces 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:

  1. Broken hermeticity and caching: A test that dials /var/run/docker.sock depends on whatever Docker daemon, cached image tags, and network proxy exist outside Bazel’s input graph.
  2. 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.
  3. 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 via http_archive with SHA-256 digests for all four platforms (linux_amd64, linux_arm64, darwin_arm64, darwin_amd64), matching production PG 16. On Linux we specifically select the gnu builds rather than musl because our CI and remote build images use glibc.
  • Redis (@redis from BCR): Compiled directly from source as a cc_binary (@redis//:redis-server and @redis//:redis-cli), with zero external downloads at test time.

Comparison diagram contrasting Testcontainers via the host Docker socket against hermetic rules_itest PostgreSQL and Redis inside Bazel 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:

  • initdb flags: --username=postgres --auth=trust --no-sync
  • postgres flags: -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_postgres runs pg_isready -h 127.0.0.1 -p <port> -U postgres until 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): In apps/suites/vitruvian-tabs/api/BUILD, :migrate is an itest_task that depends on :postgres and runs our Rust migration wrapper (migrate_deploy_bin) with a vendored, SHA-256-pinned Prisma schema-engine binary. :migrate_deploy_itest then queries _prisma_migrations against the migration directories on disk to prove every migration actually applied. The full :api_service waits on both :migrate and :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.rs keeps one shared postgres service per test target and calls fresh_database() (CREATE DATABASE t_<random>) in each #[tokio::test] so tests run concurrently without seeing each other’s rows.
  • Loopback enforcement (Redis): Every redis-server starts with --bind 127.0.0.1, and //tools/testinfra/redis:check_loopback inspects /proc/net/tcp{,6} on Linux and /usr/sbin/lsof on 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:

Two-by-two grid of four real systems bugs encountered running embedded Postgres and Redis in Bazel and how each was fixed 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.

  1. macOS postmaster crashes when fork() meets CoreFoundation: On macOS, calling system locale APIs before fork() initializes CoreFoundation background threads, causing PostgreSQL to abort immediately with "postmaster became multithreaded during startup". Forcing LC_ALL=C and LANG=C on both initdb and postgres in run_postgres.rs eliminates the multithreaded pre-fork state.
  2. The 0.75-second svcinit port race (#389): rules_itest’s svcinit reserves autoassign_port = True by binding 127.0.0.1:0, recording the port, and immediately closing the socket. Because initdb takes roughly 0.75s before postgres binds that port, two local tests running concurrently under macOS processwrapper-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.
  3. Remote execution containers run as root: BuildBuddy remote execution (RBE) runs actions inside Linux containers as UID 0. Source-built redis-server handles that fine—so redis_loopback_test runs remotely on RBE—but PostgreSQL refuses to run as root by design. Every postgres target and dependent test carries "no-remote-exec" so it executes on the CI runner’s regular non-root user instead.
  4. Testing database outages without stopping a container: rules_itest starts a service for the lifetime of a test binary and cannot pause or kill postgres mid-test. To verify reconnect and backoff behavior in apps/suites/vitruvian-relay/relay/tests/database_down.rs, pg_support::Proxy inserts a tiny Tokio TCP proxy between PgStore and Postgres with four switchable modes: Open, Down (drops connections immediately), Silent (holds sockets open without answering), and Slow(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-core consists 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_smoke builds our Backstage OCI image, starts a throwaway postgres container and Docker network, and verifies every backend plugin initializes to READY. 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

  1. 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.
  2. Expect OS and sandbox edge cases at the process boundary. Locale threads on macOS, port-reservation TOCTOU windows, and UID 0 restrictions in remote builders show up quickly once you own process startup directly.
  3. 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.