Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Setting Up Keycloak with MSSQL Server Using Docker
Jean B.
Jean B.

Posted on

Setting Up Keycloak with MSSQL Server Using Docker

In this guide, I will walk you through setting upKeycloak withMSSQL Server using Docker containers. This setup will allow you to run Keycloak with MSSQL as its backend database, useful for development or integration with other applications.

Prerequisites

Before starting, ensure you have Docker installed and running on your system. We'll be pulling images for Keycloak and MSSQL Server and setting up a Docker network to connect both containers.


Step 1: Pull Docker Images for Keycloak and MSSQL Server

Start by pulling the latest Keycloak and MSSQL Server images from their respective repositories.

docker pull quay.io/keycloak/keycloak:23.0docker pull mcr.microsoft.com/mssql/server:2019-latest
Enter fullscreen modeExit fullscreen mode

Step 2: Create a Docker Network

We will create a dedicated Docker network for Keycloak and the MSSQL Server container so they can communicate with each other.

docker network create keycloak_network
Enter fullscreen modeExit fullscreen mode

Step 3: Spin Up MSSQL Server

Next, run the MSSQL Server container. Replace"yourstrongpassword" with a strong password for thesa (System Administrator) account.

docker run\-e'ACCEPT_EULA=Y'\-e"MSSQL_SA_PASSWORD=yourstrongpassword"\-e'MSSQL_RPC_PORT=135'\-e'MSSQL_DTC_TCP_PORT=51000'\-p 1433:1433-p 135:135-p 51000:51000\--net keycloak_network\--name sqlserver1\--hostname sqlserver1\-d mcr.microsoft.com/mssql/server:2019-latest
Enter fullscreen modeExit fullscreen mode

Step 4: Connect to the MSSQL Server Container

Once the container is up and running, you can access the container's shell using the following command:

dockerexec-it sqlserver1"bash"
Enter fullscreen modeExit fullscreen mode

Login to the MSSQL server withsqlcmd:

/opt/mssql-tools/bin/sqlcmd-S localhost-U SA-P"yourstrongpassword"
Enter fullscreen modeExit fullscreen mode

Step 5: Configure Keycloak Database in MSSQL Server

After logging into the MSSQL server, run the following SQL command to install support for XA transactions (required by Keycloak):

EXECsp_sqljdbc_xa_installGO
Enter fullscreen modeExit fullscreen mode

Create a database for Keycloak:

CREATEDATABASEkeycloak;GOSELECTNamefromsys.databases;GO
Enter fullscreen modeExit fullscreen mode

Step 6: Run Keycloak Container in Development Mode

Finally, spin up the Keycloak container. Replace the values forKEYCLOAK_ADMIN_PASSWORD andKC_DB_PASSWORD with your own strong passwords.

docker run--name keycloak_mssql-d-p 8080:8080--net keycloak_network\-eKEYCLOAK_ADMIN=keyadmin\-eKEYCLOAK_ADMIN_PASSWORD=yourkeycloakpass\-eKC_DB=mssql\-eKC_DB_URL='jdbc:sqlserver://sqlserver1:1433;instanceName=sqlserver1;databaseName=keycloak;encrypt=false;'\-eKC_DB_USERNAME=sa\-eKC_DB_PASSWORD=yourstrongpassword\-eKC_HOSTNAME=localhost\-eKC_TRANSACTION_XA_ENABLED=true\  quay.io/keycloak/keycloak:23.0 start-dev
Enter fullscreen modeExit fullscreen mode

Keycloak should now be running in development mode and connected to the MSSQL database. You can access the Keycloak admin console by navigating tohttp://localhost:8080 in your browser.

Image description

Conclusion

You’ve now set up Keycloak with MSSQL Server as the backend database using Docker. This setup is ideal for development purposes and can easily be extended for production environments by adjusting the configurations. If you run into any issues, make sure to check the logs for both Keycloak and MSSQL Server containers usingdocker logs.

Let me know if you found this guide helpful!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer,.NET Core, Akka.NET, React.js, Next.js, Xstate.js, D3.js, Docker, Kubernetes, Kafka, DevOps
  • Location
    Port Vila, Vanuatu
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp