'MySQL testcontainers test can't connect to JDBC using default credentials

I'm trying to initialize MySQL test-container using Kotlin and Gradle(7.4.2), container configuration logic listed below:

class MySQL internal constructor(
    dockerImageName: DockerImageName
) : MySQLContainer<MySQL?>(dockerImageName), ConfigurableContainer {

    ...
    ... 

    companion object {
        val instance: MySQL?
        const val name = "failed-mysql"
        const val isStateful = true

        init {
            val dockerImage = "mysql:8.0.28"

            instance = ContainerBuilder.createContainer(name) {
                MySQL(DockerImageName.parse(dockerImage).asCompatibleSubstituteFor("mysql"))
                    .withUsername("root")
                    ?.withPassword("")
            }
        }
    }
}

ContainerBuilder::createContainer logic:

fun <T : GenericContainer<*>?> createContainer(name: String, builder: Supplier<T?>?): T? {
    log.info("Initializing $name")

    return builder?.get()!!.apply {
        this.withStartupTimeout(Duration.ofSeconds(startupTimeoutSeconds.toLong()))
        this.withNetworkAliases(name)
        this.withExtraHost("host.docker.internal", "host-gateway")

        if (isLongLive) {
            this.withReuse(true)
            this.withCreateContainerCmdModifier { cmd: CreateContainerCmd ->
                cmd.withName(name)
                cmd.hostConfig?.apply {
                   withRestartPolicy(RestartPolicy.unlessStoppedRestart())
                }
            }
        }
    }

The container is created successfully, the root user without a password also exists, manually I can connect without any problems, but in the console inside testcontainers internals it trying to connect via jdbc to my database, but without success (and after ~200 attempts I crash with a timeout):

2022-05-24 14:03:40 DEBUG Trying to create JDBC connection using com.mysql.cj.jdbc.Driver to jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true with properties: {password=, user=root}

I assumed that the problem was with the user сredentials, but I can connect mannualy with no problems, also if I'm trying to reconfugure it with PostgreSQL container - have no issues as well.

Additional information:

  1. docker version: 20.10.13, build a224086

  2. mysql docker image: mysql:8.0.28

  3. gradle dependencies:

    // test-containers
    api(platform("org.testcontainers:testcontainers-bom:1.16.3"))
    api("org.testcontainers:mysql")
    
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    implementation("mysql:mysql-connector-java:8.0.28")
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source