testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

MSSQLServerContainer does not allow azure-sql-edge

Open Trinition opened this issue 4 years ago • 9 comments

On an Apple M1, Microsoft's mcr.microsoft.com/mssql/server fails because the sqlcmd is not supported on ARM64; however, Microsoft makes an mcr.microsoft.com/azure-sql-edge Azure SQL Edge container that does work and claims to be fully T-SQL compatible (see here).

I managed to build my own provider that re-uses the MSSQLServerContainer, but with a different image:

/**
 * Uses a {@link MSSQLServerContainer} with a `mcr.microsoft.com/azure-sql-edge` image (default: "latest") in place of
 * the standard ` mcr.microsoft.com/mssql/server` image
 */
class AzureSqlEdgeContainerProvider extends JdbcDatabaseContainerProvider {

  private static final String NAME = 'azuresqledge'

  @Override boolean supports(String databaseType) {
    return databaseType.equals(NAME);
  }

  @Override JdbcDatabaseContainer newInstance() {
    return newInstance('latest');
  }

  @Override JdbcDatabaseContainer newInstance(String tag) {
    def taggedImageName = DockerImageName.parse("mcr.microsoft.com/azure-sql-edge")
      .withTag(tag)
      .asCompatibleSubstituteFor("mcr.microsoft.com/mssql/server") // From MSSQLServerContainer.DEFAULT_IMAGE_NAME
    return new MSSQLServerContainer(taggedImageName);
  }
}

I had to use a different databaseType name (azuresqledge instead of sqlserver) to prevent the two providers from competing. Perhaps there's a better way to avoid that competition without having to change the databaseType name.

Or perhaps there's a way to modify MSSQLServerContainer/...Provider to accept either image name.

Nonetheless, the above provider works fine with my limited, local testing thus far.

Trinition avatar Mar 31 '22 16:03 Trinition

Hey @Trinition, thanks a lot for sharing your implementation with the community 🙌

The lack of configurability of the image used by the JdbcDatabaseContainerProvider is discussed in https://github.com/testcontainers/testcontainers-java/issues/4. As I see it, if we solve this issue, it would solve this issue as well.

kiview avatar Apr 01 '22 06:04 kiview

MSSQL's JDBC Driver 10.1+ enables encrypt=true by default, but not trustServerCertificate=true. You can either add certificate trusting...

return new MSSQLServerContainer(taggedImageName)
  .withUrlParam('trustServerCertificate', 'true')

Or disable encrypt...

return new MSSQLServerContainer(taggedImageName)
  .withUrlParam('encrypt', 'false')

Trinition avatar Apr 01 '22 15:04 Trinition

@Trinition could you please elaborate more how to make image mcr.microsoft.com/azure-sql-edge work with MSSQLServerContainer. I am trying to make it work on my apple M1 pro .. without success so far...

gmelekh avatar Jan 31 '23 21:01 gmelekh

@gmelekh I apologize, but I don't recall what I did. The project I was doing this for got shelved last year, so I'm not currently working on it, and my memory has faded a lot.

Trinition avatar Feb 01 '23 15:02 Trinition

@gmelekh the little bi I was able to scape back together:

  • I implemented a subclass of JdbcDatabaseContainerProvider (see the initial post above)
  • That uses the "mcr.microsoft.com/azure-sql-edge" image
  • My connection string looks like the following (note the "azuresqledge" matches the custom provider): jdbc:tc:azuresqledge://localhost;instance=SQLEXPRESS:1434;databaseName=MyDbName;encrypt=true;trustServerCertificate=true

Trinition avatar Feb 07 '23 19:02 Trinition

I am getting this error when try to start the container

com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin

aswani-tiwari-thg avatar Nov 15 '23 12:11 aswani-tiwari-thg

It is now possible to use azure sql edge with TC. I am currently on version 1.20.*

@Bean
@ServiceConnection
fun mssqlContainer(): MSSQLServerContainer<*> {
    val imageName = DockerImageName.parse("mcr.microsoft.com/azure-sql-edge:1.0.7")
        .asCompatibleSubstituteFor("mcr.microsoft.com/mssql/server")
    return MSSQLServerContainer(imageName)
        .acceptLicense()

}

ogyct avatar Mar 04 '25 19:03 ogyct

azure-sql-edge will be retired https://azure.microsoft.com/nb-no/updates?id=azure-sql-edge-retirement and as mentioned in previous comment can me marked a compatible substitute.

eddumelendez avatar Mar 04 '25 19:03 eddumelendez

For those with M1 hitting a similar issue something along theses lines helps

 final DockerImageName imageName = DockerImageName.parse("mcr.microsoft.com/azure-sql-edge:latest")
                            .asCompatibleSubstituteFor("mcr.microsoft.com/mssql/server");

                    final MSSQLServerContainer<?> container = new MSSQLServerContainer<>(imageName).withUrlParam("trustServerCertificate", "true")
                            .acceptLicense()
                            .withConnectTimeoutSeconds(180)
                            .waitingFor(Wait.forListeningPort());
                    container.addEnv("SA_PASSWORD", container.getPassword());
                    container.withReuse(true);
Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:41 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:61 ClientConnectionId: 668853e6-d7f6-4280-90d9-c31326f42b94 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:41 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:61 ClientConnectionId: f96c426a-37f4-4808-99dd-9aa799cf6121 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:42 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:62 ClientConnectionId: ee528850-fb61-4053-bf17-d85df59f9794 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:42 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:62 ClientConnectionId: 937a5f44-3b83-4e73-8244-5751e7e52189 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:42 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:63 ClientConnectionId: 2b439520-0f2b-44dd-bb5d-ce79638da845 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:42 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:63 ClientConnectionId: 6f035d10-28aa-42ce-b29e-25245e367d17 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:42 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:64 ClientConnectionId: 816a9359-f32b-4c29-9db9-491757e82345 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:42 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:64 ClientConnectionId: 992af21e-4157-49c7-974d-3ad9cd2714aa Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
Mar 07, 2025 10:44:42 AM com.microsoft.sqlserver.jdbc.SQLServerConnection prelogin
WARNING: ConnectionID:65 ClientConnectionId: a291f4b0-f7b2-4856-be20-33b94440f232 Prelogin error: host localhost port 53620 Unexpected end of prelogin response after 0 bytes read
[main] INFO tc.mcr.microsoft.com/azure-sql-edge:latest - Container mcr.microsoft.com/azure-sql-edge:latest started in PT9.055S
[main] INFO tc.mcr.microsoft.com/azure-sql-edge:latest - Container is started (JDBC URL: jdbc:sqlserver://localhost:53620;encrypt=false;trustServerCertificate=true)
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.

atkawa7 avatar Mar 07 '25 08:03 atkawa7