Search code examples
javajunitmicronauttestcontainerstestcontainers-junit5

Test containers not deleted from docker after JUnit 5 all class tests completed


I am using the test container for integration testing and end-to-end testing of the micronaut application.

Here is the configuration for the test container

@Testcontainers
public abstract class TestContainerFixture {
    protected static final GenericContainer mongoDBContainer;
    protected static final Map<String, Object> properties;

    static {
        mongoDBContainer = new GenericContainer(DockerImageName.parse("mongo:4.0.10"))
                .withExposedPorts(27017)
                .withReuse(true);
        mongoDBContainer.start();
        properties = Map.of("mongodb.uri",
                String.format("mongodb://%s:%s", mongoDBContainer.getContainerIpAddress(), mongoDBContainer.getMappedPort(27017)));
    }

    protected ApplicationContext applicationContext;
}

A class extending the test container

public class DiscountUpdateListenerTest extends TestContainerFixture {
}

Since I am using .withReuse(true); to reuse the test container for all the other test class. If I disable the .withReuse(false) on each class when the integration test is running, the container is created which takes the longer time for the test to execute.

So, to reuse the same container I have used the feature .withReuse(true). Since the container remain there for longer period of time. So I want to remove the container on each 1-2 hours


Solution

  • The reuse feature of Testcontainers is currently in alpha state and doesn't support cleaning up the containers (yet).

    Kevin Wittek (one of the core maintainers of Testcontainers), recently shared the information that cleaning up containers and resources for reusable containers will come soon (see his message in the live chat here).

    For the time being, I'd suggest to create a basic bash/CMD script and let a cron job trigger it:

    docker system prune -f --volumes