无法使用 KeyCloak 的 JBoss docker 图像连接到 SQL 服务器

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

所以我启动了SQL服务器docker图片:

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 

使用 Microsoft SQL Management Studio 我可以连接到 localhost,1433。我创建了一个名为 keycloak 的数据库,sa 是该数据库的所有者。

现在运行 Keycloak 图像无法连接到数据库:

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

失败并出现此错误:

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.".

通过日志搜索首先出现上述错误,但随后到处都是此错误,这可能是先前错误的副作用,但至少它告诉我正确的 JDBC url 正在使用:

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)

我搜索了错误,我没有任何关于防火墙或 TCP 端口设置不正确的问题,根据 Microsoft 的说法,第一个命令会将 TCP 端口设置为 1433.

有没有人有过类似的经历,请帮帮我。 有谁知道

您的 MSSQL 不在 Keycloak 容器的 localhost 上 运行 - 所以 DB_ADDR=localhost 它不正确。它是您主机上 localhost 上的 运行。将主机网络 --network host 用于 Keycloak 容器,然后 localhost 用于 DB_ADDR 将有效。请记住,容器中的 localhost 默认情况下不是主机系统的 localhost(除非您使用的是主机网络)。

推荐文档(还有其他选项如何在没有主机网络的情况下对其进行排序):https://docs.docker.com/network/