#1
MariaDB is a reliable, open-source SQL database that's often used as a drop-in replacement for MySQL. It offers solid performance, rich SQL features, and works seamlessly with Spring Boot applications. When paired with Docker, MariaDB becomes even easier to integrate—especially when setting up development and production environments using Spring Boot’s application.yml.
This guide will walk you through setting up MariaDB with Docker for both development and Kubernetes-based production, using application.yml files and Spring profiles to manage configurations cleanly.

Step 1: Why Docker for MariaDB?

Installing MariaDB locally or on a server can be time-consuming and inconsistent across machines. Docker helps by:
  • Standardizing your development environment
  • Keeping local machines clean
  • Making testing and deployment repeatable
  • Simplifying switching between versions

Step 2: Run MariaDB in Docker (Development)

Let’s start by spinning up a local MariaDB container:
docker run -d --name mariadb-dev \
  -e MARIADB_ROOT_PASSWORD=strongpassword \
  -e MARIADB_DATABASE=dev_db \
  -e MARIADB_USER=dev_user \
  -e MARIADB_PASSWORD=dev_pass \
  -p 3306:3306 mariadb:latest
This command sets up:
  • A root password (strongpassword)
  • A default database (dev_db)
  • A non-root user (dev_user) with a password (dev_pass)
  • Port 3306 exposed for external access

Step 3: Add MariaDB Dependency in Spring Boot

To enable MariaDB support in Spring Boot, include the MySQL driver (which works for MariaDB) in your pom.xml:
<dependency>
  <groupId>org.mariadb.jdbc</groupId>
  <artifactId>mariadb-java-client</artifactId>
  <version>3.1.4</version>
</dependency>
This allows your application to connect to MariaDB using JDBC.

Step 4: Configure application-dev.yml

Create an application-dev.yml file to define your development environment:
spring:
  datasource:
    url: jdbc:mariadb://localhost:3306/dev_db
    username: dev_user
    password: dev_pass
    driver-class-name: org.mariadb.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
This configuration tells Spring Boot how to connect to the MariaDB container running on your machine.
To use this profile:
SPRING_PROFILES_ACTIVE=dev

Step 5: Kubernetes Production Setup

In production, especially when using Kubernetes, you shouldn't hardcode database values. Use ConfigMaps and Secrets to inject configuration dynamically.

Step 5.1: Create ConfigMap and Secret

ConfigMap for general DB settings:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mariadb-config
  namespace: your-namespace
data:
  mariadb_host: mariadb-service
  mariadb_port: "3306"
  mariadb_database: prod_db

Secret for credentials:

apiVersion: v1
kind: Secret
metadata:
  name: mariadb-secret
  namespace: your-namespace
type: Opaque
data:
  mariadb_user: cHJvZF91c2Vy     # base64 for 'prod_user'
  mariadb_password: c3Ryb25ncGFzcw==  # base64 for 'strongpass'

Step 5.2: Reference in Deployment YAML

Your deployment file should inject these values:
containers:
  - name: your-app
    image: your-app-image
    env:
      - name: MARIADB_HOST
        valueFrom:
          configMapKeyRef:
            name: mariadb-config
            key: mariadb_host
      - name: MARIADB_PORT
        valueFrom:
          configMapKeyRef:
            name: mariadb-config
            key: mariadb_port
      - name: MARIADB_DATABASE
        valueFrom:
          configMapKeyRef:
            name: mariadb-config
            key: mariadb_database
      - name: MARIADB_USER
        valueFrom:
          secretKeyRef:
            name: mariadb-secret
            key: mariadb_user
      - name: MARIADB_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mariadb-secret
            key: mariadb_password
      - name: SPRING_PROFILES_ACTIVE
        value: prod

Step 6: Configure application-prod.yml

Set up your production configuration file:
spring:
  datasource:
    url: jdbc:mariadb://${MARIADB_HOST}:${MARIADB_PORT}/${MARIADB_DATABASE}
    username: ${MARIADB_USER}
    password: ${MARIADB_PASSWORD}
    driver-class-name: org.mariadb.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: validate
This setup ensures your production environment reads connection values from Kubernetes at runtime.

Step 7: Using Spring Profiles

Maintain clean separation between development and production with Spring profiles:
  • application-dev.yml for local development
  • application-prod.yml for Kubernetes production
Activate the right profile using:
SPRING_PROFILES_ACTIVE=dev  # or prod
This keeps your configuration modular and environment-safe.

image quote pre code