Video in progress
You run integration tests with Testcontainers in your CI pipeline. What problems do you hit, and how do you solve them?
The main problems are no Docker on the CI runner or a Docker-in-Docker setup, re-pulling images on every build, and slow startup. The solutions: the Docker socket or a remote Docker, an image cache/mirror, and reusing containers across tests.
📖 Answer
Testcontainers needs the Docker API. If the CI runner is itself a container (a GitLab runner, the Kubernetes executor), the options are:
- mount the host's
/var/run/docker.sock: fast, but the runner effectively becomes root on the host; - a DinD service (
docker:dind): isolated, but it needs privileged mode and has no cache of its own; - Testcontainers Cloud, or a separate remote Docker host.
Privileged pods may be forbidden on Kubernetes runners, so agree on this with the platform team in advance.
For speed:
@Testcontainers
abstract class IntegrationTest {
@Container
@ServiceConnection
static final PostgreSQLContainer<?> PG =
new PostgreSQLContainer<>("postgres:16-alpine");
}
A static container is reused within one test class. With the singleton pattern (start() in a static {} block) there is a single one for the whole test suite. Together with the Spring test context cache, this cuts the build time several times over.
Two more things:
- Docker Hub rate limits hit CI often. Set up an internal registry mirror, or pull images from your own registry with
hub.image.name.prefix; - Ryuk (the cleanup container) does not work on some CI systems. If you set
TESTCONTAINERS_RYUK_DISABLED=true, make sure the runner is ephemeral, otherwise orphaned containers pile up.
@ServiceConnection has existed since Spring Boot 3.1. Before that we gave the URL by hand with @DynamicPropertySource.
Sign up to mark studied questions and save your progress.