Quick revision
The short answer
Testcontainers fits tests best: containers are defined in code, isolated per test run, and controlled by the test lifecycle. Docker Compose support fits a long-lived local environment that several services, tools and people share, described in a compose.yaml anyone can start. Many teams use both, with the same pinned image versions, and Testcontainers at development time has the extra benefit of reusing the test configuration, so the dev and test setups cannot drift apart.
What to remember
In this answer, we saw that Docker Compose support and Testcontainers both start real dependencies and wire them in through service connections, but differ in who defines the containers and how long they live. Testcontainers fits tests, with isolated containers controlled from code. Docker Compose fits a long-lived local environment shared by services, tools and people. Both should use the same pinned image versions, and Testcontainers at development time lets a single service reuse its test configuration so dev and test stay aligned.
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.
Testcontainers fits tests best: containers are defined in code, isolated per test run, and controlled by the test lifecycle. Docker Compose support fits a long-lived local environment that several services, tools and people share, described in a compose.yaml anyone can start. Many teams use both, with the same pinned image versions, and Testcontainers at development time has the extra benefit of reusing the test configuration, so the dev and test setups cannot drift apart.
Why this question is asked
When a team adopts both tools, the same question comes up in every review: why do we have a compose.yaml and a containers configuration class, and which one should a new service use? Interviewers ask it to see whether we understand what each tool is designed for, rather than picking one out of habit. In this answer, we will recap what the two have in common, look at where Testcontainers is the better fit, where Docker Compose is, why image versions must match, and how Testcontainers at development time keeps the dev setup aligned with tests.
1A Quick Recap: What the Two Have in Common
Both tools start real dependencies, such as PostgreSQL, Kafka or Redis, in containers, and both connect a Spring Boot application to them through service connections: ConnectionDetails beans that auto-configuration uses instead of connection properties. So, from the application's point of view, the result is the same; it finds a database without anyone writing spring.datasource.url.
The difference is who defines the containers and who controls their lifetime. With Docker Compose support, a compose.yaml file defines them and the docker compose command runs them. With Testcontainers, Java code defines them and the test or the application context starts and stops them.
| Question | Docker Compose support | Testcontainers |
|---|---|---|
| Defined in | compose.yaml | Java code (@Bean, @Container) |
| Lifetime | long-lived, can outlive the app | per test run or per context |
| Isolation | shared by whoever uses it | fresh containers per run |
| Used by | the app, other services, CLI tools, people | tests, and the app via TestApplication |
| Active in tests | skipped by default | the main purpose |
2Where Testcontainers Is the Better Fit
Tests need isolation and repeatability. Each run should start from a known state, and two test runs on the same CI agent must not see each other's data. Testcontainers gives exactly that: containers start with the test context, get random ports, and are removed when the run ends. We can choose per-test-class containers for full isolation or a shared container across test classes to save time.
Testcontainers is also controlled from code, so a test can do things a static file cannot: wait for a specific log line, run an init script, stop a container to test failure handling, or start a container with a different configuration for one test class.
3Where Docker Compose Is the Better Fit
A local environment for daily development has different needs. It should be long-lived, so data we entered yesterday is still there. Several things may share it: two of our services running side by side, a database client, a message browser, a front-end developer running the UI. And it may hold services that have nothing to do with Java, like a mock identity provider or an observability stack.
A compose.yaml suits this well. Anyone can start it with docker compose up, with or without the Java application. Spring Boot's Docker Compose support adds convenience on top: it starts the file when the app starts, and with lifecycle-management set to start-only it leaves the containers running between restarts.
Notice that this file uses fixed host ports on purpose, because people and tools connect to them by hand. In tests we would never do that, as fixed ports clash between parallel runs.
4Use the Same Image Versions in Both
If tests run against postgres:17 and the local environment runs postgres:15, a query that uses a newer feature passes in tests and fails locally, or the reverse. The same applies to Kafka versions and Redis versions. Each difference is a source of confusing, environment-only bugs.
So both should use the same pinned tags, and those tags should follow production. A simple way to enforce it is to keep the image names in one place, for example a small properties file or a constants class that the Testcontainers configuration reads, and a CI check that compares compose.yaml with it.
5Testcontainers at Development Time Keeps Dev and Test Aligned
Since Spring Boot 3.1, we can also run the application locally with Testcontainers, using a TestApplication class in src/test that calls SpringApplication.from(Application::main).with(ContainersConfig.class). The local application then uses exactly the container configuration the integration tests use. When someone changes the database image or adds Redis to the tests, the local setup changes with it automatically.
That makes a simple rule of thumb. For a single service whose dependencies are only for that service, Testcontainers at dev time is often enough, and it means one definition instead of two. When the local environment is shared by several services, tools or non-Java components, Docker Compose is the better home for it, and the tests keep using Testcontainers.
- Is it for automated tests?YesTestcontainersNoNext question
- Is the local environment shared with other services or tools?YesDocker Compose supportNoTestcontainers at dev time
- Either way: pin the same image versionsand follow production
3 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Can we use a compose file from Testcontainers?
Yes. Testcontainers has a ComposeContainer that starts services from a compose file inside a test, which is useful when a test needs the whole local stack. It is slower and less isolated than individual containers, so we use it for a few end-to-end tests rather than for every integration test.
Why does Docker Compose support skip tests by default?
Because a shared, long-lived environment is the opposite of what tests need. A test that connects to whatever containers happen to be running would depend on leftover data and on other runs. Boot's default, spring.docker.compose.skip.in-tests=true, pushes tests towards isolated containers.
What about CI, where Docker may be remote or restricted?
Testcontainers works with any Docker-compatible host, including a remote one set through DOCKER_HOST or a cloud service, so CI agents need no local daemon. The compose environment is mostly irrelevant in CI, because CI runs tests rather than a long-lived developer environment.
7Conclusion
In this answer, we saw that Docker Compose support and Testcontainers both start real dependencies and wire them in through service connections, but differ in who defines the containers and how long they live. Testcontainers fits tests, with isolated containers controlled from code. Docker Compose fits a long-lived local environment shared by services, tools and people. Both should use the same pinned image versions, and Testcontainers at development time lets a single service reuse its test configuration so dev and test stay aligned.
Open with
I see them as tools for two different jobs that share one mechanism, service connections. Testcontainers is my choice for tests: containers are defined in code, start fresh with the test context on random ports, and disappear afterwards, so runs are isolated and repeatable, and I can control them from the test, like stopping a container to test a failure.
Then, if they want more
Docker Compose is better for a long-lived local environment: a compose.yaml anyone can start, that keeps its data between days, and that can include things shared by several services or not related to Java at all, like a Kafka UI or a mock identity provider.
Boot's Docker Compose support starts it with the app, and with start-only the containers stay up across restarts; it is skipped in tests by default, which is right.
Whatever we use, the image versions must be the same in both and match production, otherwise we get bugs that only happen in one place, so I keep the tags in one place and check them in CI.
And for a single service with its own dependencies, I often just use Testcontainers at dev time, with a TestApplication that calls SpringApplication.from and adds the test containers config, so there is one definition and dev and test cannot drift.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.1
- 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
- Spring Boot reference
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 "Your team uses both Docker Compose support and Testcontainers. When would you choose one...": https://scrivio-interview-library.netlify.app/spring-boot/developer-experience/805c25911b/