#1
Microsoft SQL Server (MSSQL) is a robust, enterprise-grade relational database that's often used in corporate systems and backend services. If you're working with Spring Boot and want to integrate MSSQL smoothly, Docker makes the setup process simple and repeatable. Whether you're developing locally or deploying in Kubernetes, Docker paired with Spring’s configuration system keeps your environment clean and manageable.
In this article, we’ll cover how to use Docker to run MSSQL and how to connect your Spring Boot application using application.properties—with separate configurations for development and production.

Step 1: Why Use Docker for Microsoft SQL Server?

Running MSSQL in Docker helps you avoid installing the database directly on your machine or managing heavy dependencies. Docker ensures that:
  • You get consistent behavior across machines.
  • You can tear down and rebuild the database easily.
  • Your dev and prod environments remain isolated but similar.

Step 2: Running Microsoft SQL Server in Docker for Development

You can run the official Microsoft SQL Server image locally with a simple command:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=YourStrong!Passw0rd" \  -p 1433:1433 --name mssql-dev -d mcr.microsoft.com/mssql/server:2019-latest
Explanation:
  • ACCEPT_EULA=Y: Accepts Microsoft’s license terms.
  • SA_PASSWORD: Sets the password for the system administrator account.
  • -p 1433:1433: Exposes SQL Server's default port.
  • mcr.microsoft.com/mssql/server: The official Docker image.
Wait a few seconds for the database to initialize, and it will be accessible via localhost:1433.

Step 3: Add MSSQL JDBC Driver to Your Spring Boot Project

In your pom.xml, include the Microsoft JDBC driver:
<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>12.2.0.jre11</version> <!-- Adjust based on your Java version -->
</dependency>
This enables your Spring Boot app to talk to SQL Server.

Step 4: Configure application-dev.properties

For development, create an application-dev.properties file and connect to your local SQL Server instance:
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testdb
spring.datasource.username=sa
spring.datasource.password=YourStrong!Passw0rd
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.ddl-auto=update
Make sure testdb exists or adjust it to your actual development database.
To activate this configuration, set the profile:
SPRING_PROFILES_ACTIVE=dev
Spring Boot will now use the local Docker-based MSSQL instance.

Step 5: Setting Up Production Configuration (Kubernetes)

In production environments, you usually don’t want hardcoded database credentials. Instead, you can use Kubernetes ConfigMaps and Secrets to manage configuration values.

Step 5.1: Create a Kubernetes ConfigMap and Secret

First, define a ConfigMap for non-sensitive values:
apiVersion: v1
kind: ConfigMap
metadata:
  name: mssql-config
  namespace: your-namespace
data:
  mssql_host: mssql-service
  mssql_port: "1433"
  mssql_database: prod_db
Then, store sensitive values like username and password in a Kubernetes Secret:
apiVersion: v1
kind: Secret
metadata:
  name: mssql-credentials
  namespace: your-namespace
type: Opaque
data:
  mssql_user: c2E=          # base64 encoded 'sa'
  mssql_password: WW91clN0cm9uZyFQYXNzdzByZA==  # base64 encoded password

Step 5.2: Inject Config into Your Spring Boot App

Update your deployment YAML to include environment variables:
containers:
  - name: your-app
    image: your-app-image
    env:
      - name: MSSQL_HOST
        valueFrom:
          configMapKeyRef:
            name: mssql-config
            key: mssql_host
      - name: MSSQL_PORT
        valueFrom:
          configMapKeyRef:
            name: mssql-config
            key: mssql_port
      - name: MSSQL_DATABASE
        valueFrom:
          configMapKeyRef:
            name: mssql-config
            key: mssql_database
      - name: MSSQL_USER
        valueFrom:
          secretKeyRef:
            name: mssql-credentials
            key: mssql_user
      - name: MSSQL_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mssql-credentials
            key: mssql_password
      - name: SPRING_PROFILES_ACTIVE
        value: prod

Step 6: Configure application-prod.properties

Now set up your production configuration file:
sspring.datasource.url=jdbc:sqlserver://${MSSQL_HOST}:${MSSQL_PORT};databaseName=${MSSQL_DATABASE}
spring.datasource.username=${MSSQL_USER}
spring.datasource.password=${MSSQL_PASSWORD}
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.ddl-auto=validate
This ensures that Spring Boot reads connection settings from environment variables passed by Kubernetes.

Step 7: Switching Between Dev and Prod Profiles

Use Spring Boot profiles to maintain environment-specific settings:
  • application-dev.properties for local development
  • application-prod.properties for Kubernetes or cloud deployments
Control which profile is active using:
SPRING_PROFILES_ACTIVE=dev  # or prod
This cleanly separates concerns and avoids accidental misuse of production credentials during development.

image quote pre code