Dropwizard is a solid framework for building REST APIs, and pairing it with MariaDB gives you a powerful combo for reliable persistence. With Docker for local development and Kubernetes for production, you can easily manage your database configuration with environment-based YAML files. Here's how to set it up smoothly and securely.
1. Add Dependencies to pom.xml
Make sure your project includes these key dependencies:
<dependencies>
<!-- Dropwizard core -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<!-- MariaDB JDBC driver -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.version}</version>
</dependency>
<!-- Kubernetes client (optional) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
These enable Dropwizard, MariaDB connectivity, and optional Kubernetes support.
2. Run MariaDB Using Docker (Development)
Create a
docker-compose.yml
like this:
version: '3'
services:
mariadb:
image: mariadb:10.11
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: devdb
MYSQL_USER: devuser
MYSQL_PASSWORD: devpass
ports:
- "3306:3306"
Run it:
docker-compose up -d
This launches MariaDB at
localhost:3306
with a
devdb
database and login credentials ready.
3. Create config-dev.yml
for Local Setup
Add connection details in
config-dev.yml
:
server:
applicationConnectors:
- type: http
port: 8080
database:
driverClass: org.mariadb.jdbc.Driver
user: devuser
password: devpass
url: jdbc:mariadb://localhost:3306/devdb
validationQuery: SELECT 1
Extend your configuration class:
public class DbConfig {
@JsonProperty public String driverClass;
@JsonProperty public String user;
@JsonProperty public String password;
@JsonProperty public String url;
@JsonProperty public String validationQuery;
}
public class AppConfig extends Configuration {
@JsonProperty("database")
public DbConfig database = new DbConfig();
}
Initialize the database in your application:
ManagedDataSource ds = database.build(environment.metrics(), "db");
environment.lifecycle().manage(ds);
Run locally:
java -jar target/myapp.jar server config-dev.yml
4. Prepare Kubernetes for Production
Avoid hardcoding credentials by using Kubernetes ConfigMaps and Secrets.
Run:
kubectl create configmap mariadb-config \
--from-literal=DB_URL=jdbc:mariadb://mariadb-svc:3306/proddb \
--from-literal=DB_DRIVER=org.mariadb.jdbc.Driver \
--from-literal=DB_VALIDATION_QUERY=SELECT 1
kubectl create secret generic mariadb-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodpass
5. Define config-prod.yml
for Secure Production
Point the YAML to environment variables:
server:
applicationConnectors:
- type: http
port: 8080
database:
driverClass: ${DB_DRIVER}
user: ${DB_USER}
password: ${DB_PASSWORD}
url: ${DB_URL}
validationQuery: ${DB_VALIDATION_QUERY}
6. Kubernetes Deployment Snippet
Ensure environment variables are set from your ConfigMap and Secret:
env:
- name: DB_URL
valueFrom:
configMapKeyRef:
name: mariadb-config
key: DB_URL
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: mariadb-config
key: DB_DRIVER
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: mariadb-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: mariadb-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mariadb-secret
key: DB_PASSWORD
Run in Kubernetes with:
java -jar myapp.jar server config-prod.yml
7. Why This Approach Works
- Docker setup provides a quick and isolated dev environment.
- Kubernetes secrets/configMaps keep credentials secure and scalable.
- Two config files (
config-dev.yml
and config-prod.yml
) keep environments clean and isolated.
- Ease of transition from local testing to production with minimal changes.
You now have a robust, secure foundation for using MariaDB with Dropwizard—fast iteration locally, secure deployments in Kubernetes, and clean separation of configuration per environment. Whether you're caching data, persisting user sessions, or handling business data, this architecture is ready to scale with confidence.
image quote pre code