How to map a volume to an Embedded MariaDB Container – Java TestContainers with Spring

  docker, java, mariadb, spring, testcontainers

I’m using the following library to initiate embedded maria DB container from a Java Spring app:

com.playtika.testcontainers.embedded-mariadb

https://mvnrepository.com/artifact/com.playtika.testcontainers/embedded-mariadb

This class from the 3rd party is used to initiate the container as a Spring bean:

@Slf4j
@Configuration
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
@ConditionalOnProperty(name = "embedded.mariadb.enabled", matchIfMissing = true)
@EnableConfigurationProperties(MariaDBProperties.class)
public class EmbeddedMariaDBBootstrapConfiguration {
    @Bean(name = BEAN_NAME_EMBEDDED_MARIADB, destroyMethod = "stop")
    public MariaDBContainer mariadb(ConfigurableEnvironment environment,
                                    MariaDBProperties properties) throws Exception {
        log.info("Starting mariadb server. Docker image: {}", properties.dockerImage);

        MariaDBContainer mariadb =
                new MariaDBContainer<>(properties.dockerImage)
                        .withEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "yes")
                        .withUsername(properties.getUser())
                        .withPassword(properties.getPassword())
                        .withDatabaseName(properties.getDatabase())
                        .withCommand(
                                "--character-set-server=" + properties.getEncoding(),
                                "--collation-server=" + properties.getCollation(),
                                "--max_allowed_packet=" + properties.getMaxAllowedPacket())
                        .withExposedPorts(properties.port)
                        .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withCapAdd(Capability.NET_ADMIN))
                        .withInitScript(properties.initScriptPath);
        mariadb = (MariaDBContainer) configureCommonsAndStart(mariadb, properties, log);
        registerMariadbEnvironment(mariadb, environment, properties);
        return mariadb;
    }

    private void registerMariadbEnvironment(MariaDBContainer mariadb,
                                            ConfigurableEnvironment environment,
                                            MariaDBProperties properties) {
        Integer mappedPort = mariadb.getMappedPort(properties.port);
        String host = mariadb.getContainerIpAddress();

        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("embedded.mariadb.port", mappedPort);
        map.put("embedded.mariadb.host", host);
        map.put("embedded.mariadb.schema", properties.getDatabase());
        map.put("embedded.mariadb.user", properties.getUser());
        map.put("embedded.mariadb.password", properties.getPassword());

        String jdbcURL = "jdbc:mysql://{}:{}/{}";
        log.info("Started mariadb server. Connection details: {}, " +
                "JDBC connection url: " + jdbcURL, map, host, mappedPort, properties.getDatabase());

        MapPropertySource propertySource = new MapPropertySource("embeddedMariaInfo", map);
        environment.getPropertySources().addFirst(propertySource);
    }
}

This class creates the MariaDBContainer class.

MariaDBContainer extends GenericContainer, and from GenericContainer class a volume mapping is available with this code:

new GenericContainer(...)
        .withClasspathResourceMapping("redis.conf",
                                      "/etc/redis.conf",
                                      BindMode.READ_ONLY)

The problem is that I can’t modify EmbeddedMariaDBBootstrapConfiguration, it is a part of the com.playtika.testcontainers.embedded-mariadb 3rd party.

Do you have any ideas how can I map a volume to a maria db embedded container?

Source: Docker Questions

LEAVE A COMMENT