Quick revision
The short answer
Depending on Testcontainers means every build depends on a container runtime, on pulling images, and on a library that changes between major versions. Over thousands of builds those become real costs: CI design and security around the Docker socket, time and bandwidth for image pulls, test images drifting away from production versions, and painful coordinated upgrades. We manage them as a platform concern: an internal registry mirror with approved images, versions tied to the production version policy, a shared test library that absorbs upgrades, and metrics for test duration and container start time.
What to remember
In this answer, we saw that Testcontainers turns into infrastructure once thousands of builds depend on it. The container runtime shapes CI design, security and laptops; image pulls go through an internal mirror of approved images; test images follow the production version policy; major upgrades go through a shared starter; and test duration and container start time keep the costs visible.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Depending on Testcontainers means every build depends on a container runtime, on pulling images, and on a library that changes between major versions. Over thousands of builds those become real costs: CI design and security around the Docker socket, time and bandwidth for image pulls, test images drifting away from production versions, and painful coordinated upgrades. We manage them as a platform concern: an internal registry mirror with approved images, versions tied to the production version policy, a shared test library that absorbs upgrades, and metrics for test duration and container start time.
Why this question is asked
Testcontainers is one of those tools that wins a team over in an afternoon. A developer adds a few lines, a real PostgreSQL starts for the test, and a whole class of 'works on H2, fails in production' bugs disappears. The question here is what happens five years later, when 300 repositories and 4,000 builds a day rely on it. Interviewers ask it at this level to see whether we notice a convenience turning into infrastructure. In this answer, we will recap what Testcontainers actually needs from the machine it runs on, then go through the four main costs and risks one at a time, see the controls a platform team puts around each, and finish with the metrics that tell us whether the controls are working.
1A Quick Recap of What Testcontainers Needs
Testcontainers is a Java library that starts Docker containers from test code. When a test asks for a PostgreSQLContainer, the library talks to a container runtime through the Docker API, pulls the image if needed, starts it on a random free port, waits until the database answers and hands the test a JDBC URL. A helper container called Ryuk removes everything afterwards, even if the JVM crashed.
Notice what this quietly assumes: a container runtime reachable from the build, an image that must be downloaded from a registry, and test code coupled to a library API the whole organisation now shares. Each assumption is cheap for one team and expensive for a hundred.
2Cost 1: Every Build Needs a Container Runtime
The first cost is structural. A build that used to need only a JDK now needs permission to start containers. On CI that shapes the whole agent design. The common options are mounting the host's Docker socket into the build container, running Docker-in-Docker as a privileged sidecar, or pointing builds at a separate remote Docker host. Mounting the socket is fast, but anything that can talk to the Docker socket can start a privileged container and effectively become root on the host. Docker-in-Docker isolates builds better but needs privileged mode and loses its image cache between jobs. A remote host keeps agents unprivileged but adds a network hop and a shared resource that can run out of capacity.
Developer machines feel this too: Docker Desktop licensing in larger companies, alternatives such as Podman or Colima that behave slightly differently, and Apple silicon, where an image without an arm64 variant runs under emulation and can be several times slower. So standardising on Testcontainers is also a decision about laptops, CI agents and the security model.
I would make one approved CI pattern, templated, and treat others as exceptions that need a security review; often that is isolated Docker hosts per agent pool, or rootless runtimes where the platform supports them.
3Cost 2: Image Pulls Take Time and Bandwidth
Every fresh CI agent starts with an empty image cache. A PostgreSQL image is around 150 MB to 400 MB depending on the variant, Kafka and Elasticsearch images are bigger, and a service that tests against four of them may pull a gigabyte before the first test runs. Across thousands of builds a day that is a measurable share of build time and bandwidth, and public registries apply rate limits, so on a busy day builds fail for reasons unrelated to the code.
The control is an internal registry mirror, a pull-through cache that sits inside our network and serves images that have already been pulled once. Testcontainers can be told to rewrite every image name so that it goes through the mirror, without changing the test code.
The mirror also gives us a place to enforce an approved list. Only images that have been scanned and accepted are available, so a test cannot quietly pull an unknown image from the internet. For images with a different name inside the company, Testcontainers supports a custom ImageNameSubstitutor class that maps one name to another; the exact configuration key for registering it is worth checking against your library version.
4Risk 1: Test Images Drift Away From Production
The promise of Testcontainers is 'we test against the real thing'. That promise breaks silently when the test uses postgres:latest or an image tag someone typed three years ago. Suppose production runs PostgreSQL 17 and a test still pins postgres:13. A migration using a newer feature fails in the test and someone works around it, or a query passes on one version and misbehaves on the other. Either way, the test no longer tells the truth.
The fix is to tie test images to the same version policy as production. In practice I like one shared place that holds the approved image versions, published as part of the internal test library and updated by the same automation (Renovate or Dependabot) that tracks production versions. When the database platform team upgrades production to a new major version, they bump the shared constant, and every service's tests follow on their next dependency update.
Never use latest in a test: two builds of the same commit could then test against different databases.
5Risk 2: Library Upgrades Across Major Versions
Testcontainers itself evolves: artifacts and packages get renamed, JUnit 4 support was separated out and older container classes deprecated; the 2.x line is the latest example, so check the release notes for your version. Spring Boot added @ServiceConnection in 3.1, which changed what a typical test looks like. When 300 repositories each wrote their setup by hand, a major upgrade becomes 300 small migrations.
The control is to put a shared layer between services and the library. A small internal test starter owns the container definitions, the image names and the Spring wiring. Services depend on the starter, not on the library details. A major upgrade then means changing the starter once, publishing a new version, and running an automated refactoring (OpenRewrite recipes work well here) across repositories for the few lines that still touch the library directly. We plan it like any platform migration, with a window, a dashboard and an end-of-support date.
6Measuring It: Test Duration and Container Start Time
None of the controls matter if we cannot see the costs. I would track a small set of numbers per repository and across the organisation: total test duration, the time from build start until the first container is ready, image pull time, container start time per image, and the number of builds that failed for infrastructure reasons such as a pull error or a runtime that was not reachable. Most CI systems can export step timings, and a JUnit extension or a Testcontainers log listener can record when each container started and how long it took to become ready.
| Cost or risk | Control | Metric |
|---|---|---|
| Container runtime on every build | One approved CI pattern, security reviewed | Infra failures per 1,000 builds |
| Image pulls | Internal pull-through mirror, approved list | Pull time per build, mirror hit rate |
| Version drift | Shared approved image versions tied to prod | Services on non-approved versions |
| Major library upgrades | Internal test starter, automated recipes | Repositories still on the old version |
| Slow suites | Shared containers per JVM, lighter images | Container start time, test duration |
These numbers turn arguments into decisions. If a service's container start time grew from 8 to 45 seconds, we can find the heavier image that caused it. If pull time dominates, the mirror is not being used. If a build starts 12 containers, sharing one container per test JVM often cuts minutes.
7Follow-up questions
Should we use reusable containers in CI to save time?
Generally no. Reuse keeps a container running between test runs and is meant for a developer's own machine; it is opt-in through the user's Testcontainers configuration file plus withReuse(true). On CI every build should start clean, so the better saving there is one container per test JVM, shared across test classes.
Is mounting the Docker socket into CI builds acceptable?
It can be, but only with the risk understood. Access to the Docker socket is effectively root access to that host, because a process can start a privileged container that mounts the host filesystem. If the agents run only trusted code from our own repositories and each agent host is isolated and short-lived, many organisations accept it. If agents run code from forks or external contributors, it is not acceptable, and we need Docker-in-Docker, a rootless runtime or a separate Docker host per job.
8Conclusion
In this answer, we saw that Testcontainers turns into infrastructure once thousands of builds depend on it. The container runtime shapes CI design, security and laptops; image pulls go through an internal mirror of approved images; test images follow the production version policy; major upgrades go through a shared starter; and test duration and container start time keep the costs visible.
Open with
I would start by saying Testcontainers is worth it, because it lets us test against the real database and broker, but at scale it becomes infrastructure and needs an owner.
Then, if they want more
The first cost is that every build needs a container runtime. That drives CI design, and it is a security question: access to the Docker socket is root-equivalent, so I would define one approved pattern, such as isolated Docker hosts per agent pool or rootless runtimes, and document it. Second, image pulls.
Thousands of builds pulling gigabytes cost time and hit public rate limits, so I put an internal pull-through mirror in front and use the image name prefix setting so tests pull through it, limited to scanned, approved images. Third, drift.
Tests must use the same major version as production, so approved image versions live in one shared place that is bumped with production, and latest is never used. Fourth, upgrades.
Major Testcontainers or Spring Boot changes become hundreds of small migrations unless services depend on a small internal test starter, so we change it once and use automated refactoring for the rest. Finally, I measure test duration, container start time, pull time and infrastructure failures, so regressions are visible.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 4, PostgreSQL 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Testcontainers makes it easy to run real infrastructure in tests. Over years, what costs...": https://scrivio-interview-library.netlify.app/java/integration-testing/4a0059513b/