When you're building a Dropwizard application that needs a robust relational database, IBM Db2 is a solid choice. Whether you're working locally or deploying in production, this guide walks you through setting up Db2 with Docker for development and Kubernetes for secure, scalable production environments. We’ll also show how to keep your setup clean using
config-dev.yml
for development and
config-prod.yml
for production.
1. Add Dependencies in pom.xml
First, update your Maven build to include necessary libraries:
<dependencies>
<!-- Dropwizard core -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<!-- IBM Db2 JDBC driver -->
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>jcc</artifactId>
<version>${db2.jcc.version}</version>
</dependency>
<!-- Kubernetes client (optional but useful) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
You now have Dropwizard, the IBM Db2 JDBC driver, and optional Kubernetes support for production-grade deployment.
2. Run Db2 Locally with Docker
For development, let’s spin up Db2 using Docker:
version: '3'
services:
db2:
image: icr.io/db2:latest
environment:
DB2INST1_PASSWORD: db2inst1pwd
LICENSE: accept
ports:
- "50000:50000"
command: db2start
Run the container:
docker-compose up -d
Db2 will be available via
localhost:50000
, with credentials you specified (
db2inst1/db2inst1pwd
).
3. Create config-dev.yml
for Development
Your local configuration might look like:
server:
applicationConnectors:
- type: http
port: 8080
database:
driverClass: com.ibm.db2.jcc.DB2Driver
user: db2inst1
password: db2inst1pwd
url: jdbc:db2://localhost:50000/sample
validationQuery: SELECT 1 FROM sysibm.sysdummy1
Map properties using a config POJO:
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();
}
In your
Application
class, initialize Db2:
ManagedDataSource ds = config.database.build(environment.metrics(), "db");
environment.lifecycle().manage(ds);
Run locally with:
java -jar target/myapp.jar server config-dev.yml
Your Dropwizard app will now connect to the local Db2 instance for development.
4. Configure Kubernetes for Production
When deploying, avoid hardcoding credentials. Use Kubernetes ConfigMaps and Secrets:
kubectl create configmap db2-config \
--from-literal=DB_URL=jdbc:db2://db2-service:50000/proddb \
--from-literal=DB_DRIVER=com.ibm.db2.jcc.DB2Driver \
--from-literal=DB_VALIDATION_QUERY=SELECT+1+FROM+sysibm.sysdummy1
kubectl create secret generic db2-secret \
--from-literal=DB_USER=db2prod \
--from-literal=DB_PASSWORD=prodPassw0rd
These store your production database URL and credentials securely.
5. Create config-prod.yml
for Production Use
Use environment variables in your production config:
server:
applicationConnectors:
- type: http
port: 8080
database:
driverClass: ${DB_DRIVER}
user: ${DB_USER}
password: ${DB_PASSWORD}
url: ${DB_URL}
validationQuery: ${DB_VALIDATION_QUERY}
Dropwizard will substitute these at run time using Kubernetes-provided values.
6. Kubernetes Deployment Snippet
Include the environment variables in your deployment manifest:
env:
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: db2-config
key: DB_DRIVER
- name: DB_URL
valueFrom:
configMapKeyRef:
name: db2-config
key: DB_URL
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: db2-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: db2-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db2-secret
key: DB_PASSWORD
Deploy and start your app with:
java -jar myapp.jar server config-prod.yml
Now your Dropwizard application connects securely to Db2 in production.
7. Why This Setup Makes Sense
- Quick local setup: Docker spins up Db2 fast for clean testing.
- Secure production: Credentials are hidden in Kubernetes resources—not in code.
- Clean configs: Separate dev and prod YAML keep things tidy and manageable.
- Future-ready: Easily extend this to pool connections or scale your database tier.
Mixing Dropwizard with IBM Db2 using this pattern gives you a robust foundation for both development and production. By localizing environment setups and applying Kubernetes best practices, you're primed for real-world use—whether you're building an enterprise API, transitioning to the cloud, or just experimenting with Db2.
image quote pre code