Search code examples
sql-serverdockerjdbcjbosskeycloak

Unable to connect to SQL server wuth JBoss docker image for KeyCloak


So I have started SQL Server docker image:

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Test@123" \
-p 1433:1433 --name sql1 -h sql1 -d mcr.microsoft.com/mssql/server:2019-latest 

And using Microsoft SQL Management Studio I can connect to localhost,1433. I have created a database named keycloak and sa is the owner of the database.

Now running Keycloak image as below fails to connect to the database:

docker run --rm --name keycloak -p 8080:8080 -e DB_VENDOR=mssql -e DB_USER=sa \
-e DB_PASSWORD=Test@123 -e DB_ADDR=localhost -e DB_PORT=1433 -e DB_DATABASE=keycloak \
-e KEYCLOAK_USER=admin  -e KEYCLOAK_PASSWORD=admin jboss/keycloak

It fails with this error:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. 
Error: "Connection refused (Connection refused). 
Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. 
Make sure that TCP connections to the port are not blocked by a firewall.".

Searching through the logs the above error happens first but then there is this error all over the place, which may be the side effect of the previous error but at least it tells me that the correct JDBC url is being used:

ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 20) WFLYCTL0403: Unexpected failure during execution of the following operation(s): [{
    "operation" => "add",
    "address" => [("subsystem" => "security")]
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "other")
    ],
    "cache-type" => "default"
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "other"),
        ("authentication" => "classic")
    ]
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "other"),
        ("authentication" => "classic"),
        ("login-module" => "Remoting")
    ],
    "code" => "Remoting",
    "flag" => "optional",
    "module-options" => {"password-stacking" => "useFirstPass"}
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "other"),
        ("authentication" => "classic"),
        ("login-module" => "RealmDirect")
    ],
    "code" => "RealmDirect",
    "flag" => "required",
    "module-options" => {"password-stacking" => "useFirstPass"}
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jboss-web-policy")
    ],
    "cache-type" => "default"
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jboss-web-policy"),
        ("authorization" => "classic")
    ]
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jboss-web-policy"),
        ("authorization" => "classic"),
        ("policy-module" => "Delegating")
    ],
    "code" => "Delegating",
    "flag" => "required"
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jaspitest")
    ],
    "cache-type" => "default"
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jaspitest"),
        ("authentication" => "jaspi")
    ]
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jaspitest"),
        ("authentication" => "jaspi"),
        ("login-module-stack" => "dummy")
    ]
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jaspitest"),
        ("authentication" => "jaspi"),
        ("login-module-stack" => "dummy"),
        ("login-module" => "Dummy")
    ],
    "code" => "Dummy",
    "flag" => "optional"
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jaspitest"),
        ("authentication" => "jaspi"),
        ("auth-module" => "Dummy")
    ],
    "code" => "Dummy"
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jboss-ejb-policy")
    ],
    "cache-type" => "default"
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jboss-ejb-policy"),
        ("authorization" => "classic")
    ]
}, {
    "operation" => "add",
    "address" => [
        ("subsystem" => "security"),
        ("security-domain" => "jboss-ejb-policy"),
        ("authorization" => "classic"),
        ("policy-module" => "Delegating")
    ],
    "code" => "Delegating",
    "flag" => "required"
}]: java.lang.RuntimeException: WFLYCTL0195: Interrupted awaiting transaction commit or rollback
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.ParallelBootOperationStepHandler$ParallelBootTransactionControl.operationPrepared(ParallelBootOperationStepHandler.java:458)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.ModelController$OperationTransactionControl.operationPrepared(ModelController.java:131)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.AbstractOperationContext.executeDoneStage(AbstractOperationContext.java:874)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.AbstractOperationContext.processStages(AbstractOperationContext.java:805)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.AbstractOperationContext.executeOperation(AbstractOperationContext.java:468)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.ParallelBootOperationStepHandler$ParallelBootTask.run(ParallelBootOperationStepHandler.java:384)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:829)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.JBossThread.run(JBossThread.java:513)

[0m[31m06:20:09,064 ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 18) WFLYCTL0403: Unexpected failure during execution of the following operation(s): [{
    "operation" => "add",
    "address" => [("subsystem" => "datasources")]
}, {
    "operation" => "add",
    "jndi-name" => "java:jboss/datasources/ExampleDS",
    "enabled" => true,
    "use-java-context" => true,
    "statistics-enabled" => expression "${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}",
    "address" => [
        ("subsystem" => "datasources"),
        ("data-source" => "ExampleDS")
    ],
    "connection-url" => "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",
    "driver-name" => "h2",
    "user-name" => "sa",
    "password" => "sa"
}, {
    "operation" => "add",
    "jndi-name" => "java:jboss/datasources/KeycloakDS",
    "enabled" => true,
    "use-java-context" => true,
    "use-ccm" => true,
    "address" => [
        ("subsystem" => "datasources"),
        ("data-source" => "KeycloakDS")
    ],
    "connection-url" => expression "jdbc:sqlserver://${env.DB_ADDR:mssql}:${env.DB_PORT:1433};databaseName=${env.DB_DATABASE:keycloak};sendStringParametersAsUnicode=false;${env.JDBC_PARAMS:}",
    "driver-name" => "sqlserver",
    "flush-strategy" => "IdleConnections",
    "user-name" => expression "${env.DB_USER:keycloak}",
    "password" => expression "${env.DB_PASSWORD:password}",
    "check-valid-connection-sql" => "SELECT 1",
    "background-validation" => true,
    "background-validation-millis" => 60000L
}, {
    "operation" => "add",
    "driver-name" => "h2",
    "driver-major-version" => undefined,
    "driver-minor-version" => undefined,
    "driver-module-name" => "com.h2database.h2",
    "address" => [
        ("subsystem" => "datasources"),
        ("jdbc-driver" => "h2")
    ],
    "driver-xa-datasource-class-name" => "org.h2.jdbcx.JdbcDataSource"
}, {
    "operation" => "add",
    "driver-name" => "sqlserver",
    "driver-major-version" => undefined,
    "driver-minor-version" => undefined,
    "driver-module-name" => "com.microsoft.sqlserver.jdbc",
    "address" => [
        ("subsystem" => "datasources"),
        ("jdbc-driver" => "sqlserver")
    ],
    "driver-xa-datasource-class-name" => "com.microsoft.sqlserver.jdbc.SQLServerXADataSource"
}]: java.lang.RuntimeException: WFLYCTL0195: Interrupted awaiting transaction commit or rollback
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.ParallelBootOperationStepHandler$ParallelBootTransactionControl.operationPrepared(ParallelBootOperationStepHandler.java:458)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.ModelController$OperationTransactionControl.operationPrepared(ModelController.java:131)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.AbstractOperationContext.executeDoneStage(AbstractOperationContext.java:874)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.AbstractOperationContext.processStages(AbstractOperationContext.java:805)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.AbstractOperationContext.executeOperation(AbstractOperationContext.java:468)
    at org.jboss.as.controller@15.0.1.Final//org.jboss.as.controller.ParallelBootOperationStepHandler$ParallelBootTask.run(ParallelBootOperationStepHandler.java:384)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:829)
    at org.jboss.threads@2.4.0.Final//org.jboss.threads.JBossThread.run(JBossThread.java:513)

I searched for the error and I don't have any issue in regards to firewall or TCP port not being set properly, according to Microsoft the first command will set TCP port to 1433.

Has anyone had similar experience, please help me on this. Does anyone know


Solution

  • Your MSSQL is not running on the localhost in the Keycloak container - so DB_ADDR=localhost it not correct. It is running on the localhost on your host machine. Use host network --network host for Keycloak container, then localhost for DB_ADDR will be valid. Keep in mind localhost in the container is not a localhost of host system by default (unless you are using host network).

    Recommended doc (there are also other options how to sort it without host network): https://docs.docker.com/network/