Search code examples
testcontainersspring-boot-testcontainers

How do you configure the logging driver for container using testcontainers?


When I write a docker-compose file I can insert a logging section like so:

logging:
  options:
    max-size: 10m
    max-file: "3"

How can I congfigure my GenericContainer to have a logging driver configuration like above?

I ask because with Spring Boot's new testcontainer support it is very easy to load up infra components in containers (Kafka e.g.) for a local dev env. Leaving these infra containers on overnight has left me with a full up hard drive. Stopping the running JVM reaps the containers and gets the space back but I would like to know if there is a known way to configure as above.


Solution

  • Apparently you can achieve this using the 'create container cmd modifier':

        private static final MySQLContainer<?> DB;
    
        static {
            try (final MySQLContainer<?> tmpDb = new MySQLContainer<>(DockerImageName.parse("mysql").withTag("5.7.42"))) {
                tmpDb.withLogConsumer(new Slf4jLogConsumer(logger));
                tmpDb.withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withLogConfig(new LogConfig(LogConfig.LoggingType.LOCAL, Map.of(
                        "max-size", "10m",
                        "max-file", "3"
                ))));
                DB = tmpDb;
            }
            DB.start();
        }
    

    This should work for a GenericContainer as well - withCreateContainerCmdModifier is a method from that class.