#1
When working with Spring Boot and MySQL, managing configuration across environments is crucial. Using Docker for local development and Kubernetes for production ensures consistency and scalability. With Spring Boot’s profile-based configuration using application.properties, you can easily maintain clean, separated settings for dev and prod environments.
This article focuses on setting up MySQL in Spring Boot using Docker locally and configuring it for Kubernetes in production. We’ll rely on two separate files—application-dev.properties and application-prod.properties—to manage configurations clearly.

1. Add Required Dependencies

To begin, ensure your pom.xml has the following dependencies:
<dependencies>
  <!-- Spring Boot Web and JPA -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

  <!-- MySQL JDBC Driver -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>
</dependencies>
These provide the base for your Spring Boot application to connect to MySQL.

2. Running MySQL with Docker for Local Development

To quickly spin up MySQL locally, use this Docker command:
docker run --name mysql-dev \
  -e MYSQL_ROOT_PASSWORD=devpass \
  -e MYSQL_DATABASE=devdb \
  -e MYSQL_USER=devuser \
  -e MYSQL_PASSWORD=devpass \
  -p 3306:3306 \
  -d mysql:8.0
This sets up a containerized MySQL instance with a user named devuser and password devpass, accessible at localhost:3306.

3. Configure application-dev.properties

Inside your src/main/resources/ directory, create a application-dev.properties file:
spring.datasource.url=jdbc:mysql://localhost:3306/devdb
spring.datasource.username=devuser
spring.datasource.password=devpass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
To run your app using the dev profile:
SPRING_PROFILES_ACTIVE=dev ./mvnw spring-boot:run
This configuration is tuned for local development and will automatically update your schema.

4. Production Configuration for Kubernetes

In a production environment, storing sensitive values directly in config files is not safe. Kubernetes lets you manage these values through ConfigMaps and Secrets.

4.1: Create a ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
data:
  spring.datasource.url: jdbc:mysql://mysql-prod-service:3306/proddb
  spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
  spring.jpa.hibernate.ddl-auto: validate
  spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL8Dialect

4.2: Create a Secret

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  spring.datasource.username: cHJvZHVzZXI=  # base64 for 'produser'
  spring.datasource.password: cHJvZHBhc3M=  # base64 for 'prodpass'

5. Configure application-prod.properties

Use placeholders in your production configuration to pull values from environment variables:
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
spring.datasource.driver-class-name=${SPRING_DATASOURCE_DRIVER_CLASS_NAME}

spring.jpa.hibernate.ddl-auto=${SPRING_JPA_HIBERNATE_DDL_AUTO}
spring.jpa.properties.hibernate.dialect=${SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT}

6. Deploy Spring Boot App to Kubernetes

Here’s how to set up your Spring Boot deployment in Kubernetes:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
        - name: springboot-app
          image: your-image
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: prod
            - name: SPRING_DATASOURCE_URL
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: spring.datasource.url
            - name: SPRING_DATASOURCE_DRIVER_CLASS_NAME
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: spring.datasource.driver-class-name
            - name: SPRING_JPA_HIBERNATE_DDL_AUTO
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: spring.jpa.hibernate.ddl-auto
            - name: SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: spring.jpa.properties.hibernate.dialect
            - name: SPRING_DATASOURCE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: spring.datasource.username
            - name: SPRING_DATASOURCE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: spring.datasource.password
By using Docker and Kubernetes with Spring Boot's profile-based configuration, you can separate your local development setup from your production environment. This makes the system easier to maintain, improves security by keeping secrets out of source code, and enhances portability.
Using application-dev.properties and application-prod.properties keeps your configurations organized and your deployments predictable—whether you’re building a simple API or scaling a microservices platform.

image quote pre code