#1
MariaDB is a popular open-source relational database that many developers use as a drop-in replacement for MySQL. It’s reliable, performant, and plays nicely with Spring Boot. By combining MariaDB with Docker and Spring Boot’s application.properties, you can create a simple, flexible setup that works both locally and in Kubernetes production environments.
This article will walk you through setting up MariaDB with Docker for development, configuring Spring Boot using application.properties, and preparing for production deployments using Kubernetes and environment variables.

Step 1: Why Docker for MariaDB?

Using Docker to run MariaDB has several advantages:
  • Quick setup: Launch a container with a single command.
  • Portability:The same configuration works across machines and environments.
  • No installation clutter: No need to install MariaDB directly on your system.
  • Clean transitions to production: Docker-based setups mirror cloud deployments more closely.

Step 2: Run MariaDB in Docker for Development

You can get a development-ready MariaDB container up and running using this command:
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 starts a MariaDB container with:
  • A root password (strongpassword)
  • A user (dev_user) and password (dev_pass)
  • A database called dev_db
Port 3306 is exposed, allowing your Spring Boot app to connect.

Step 3: Add MariaDB Dependency in Spring Boot

To connect to MariaDB from Spring Boot, include the MariaDB driver in your pom.xml:
<dependency>
  <groupId>org.mariadb.jdbc</groupId>
  <artifactId>mariadb-java-client</artifactId>
  <version>3.1.4</version>
</dependency>
This ensures Spring can talk to the MariaDB database over JDBC.

Step 4: Configure application-dev.properties

Create a file called application-dev.properties and add the following:
spring.datasource.url=jdbc:mariadb://localhost:3306/dev_db
spring.datasource.username=dev_user
spring.datasource.password=dev_pass
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
br/These settings tell Spring how to connect to your MariaDB instance running in Docker.
To use this dev config, activate the profile:
SPRING_PROFILES_ACTIVE=dev
Spring Boot will automatically load application-dev.properties.

Step 5: Prepare for Kubernetes Deployment (Production)

In production, especially in Kubernetes, hardcoding credentials or connection strings is discouraged. Use Kubernetes ConfigMaps and Secrets to pass configuration values securely and dynamically.

Step 5.1: Create ConfigMap and Secret

ConfigMap for general values:

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

Inject these values into your Spring Boot app via environment variables:
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.properties

Create a application-prod.properties file to support production:
spring.datasource.url=jdbc:mariadb://${MARIADB_HOST}:${MARIADB_PORT}/${MARIADB_DATABASE}
spring.datasource.username=${MARIADB_USER}
spring.datasource.password=${MARIADB_PASSWORD}
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=validate
These values will be replaced by Kubernetes at runtime via environment variable injection.

Step 7: Switching Between Profiles

Use Spring Boot’s profile feature to toggle between environments:
  • application-dev.properties for local development
  • application-prod.properties for production
To switch, set:
SPRING_PROFILES_ACTIVE=dev
Or configure it in your Kubernetes deployment YAML for production.

image quote pre code