Docker Deployment Guide¶
This guide explains how to build and run the Spring Boot Circuit Breaker API using Docker and Docker Compose.
Prerequisites¶
- Docker 20.10 or higher
- Docker Compose 2.0 or higher
- At least 2GB of available RAM
Quick Start¶
Using Docker Compose (Recommended)¶
-
Build and start all services:
-
Check service status:
-
View logs:
-
Access the application:
- API: http://localhost:8080
- Swagger UI: http://localhost:8080/swagger-ui.html
- API Docs: http://localhost:8080/api-docs
-
Health Check: http://localhost:8080/actuator/health
-
Stop services:
-
Stop and remove volumes (clean slate):
Building the Docker Image¶
Build with Docker Compose¶
Build with Docker directly¶
Build with specific tag¶
Running the Application¶
Option 1: Docker Compose (with MongoDB)¶
Option 2: Docker only (without MongoDB)¶
docker run -d \
--name sprboot-api \
-p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=docker \
sprboot-api-cktbrkr:latest
Option 3: Docker with external MongoDB¶
docker run -d \
--name sprboot-api \
-p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=docker \
-e SPRING_DATA_MONGODB_URI=mongodb://your-mongo-host:27017/sampledb \
sprboot-api-cktbrkr:latest
Configuration¶
Environment Variables¶
The application supports the following environment variables:
| Variable | Default | Description |
|---|---|---|
SPRING_PROFILES_ACTIVE |
docker |
Active Spring profile |
SPRING_DATA_MONGODB_URI |
mongodb://mongodb:27017/sampledb |
MongoDB connection URI |
SPRING_DATA_MONGODB_DATABASE |
sampledb |
MongoDB database name |
SPRING_SECURITY_USER_NAME |
user |
Basic auth username |
SPRING_SECURITY_USER_PASSWORD |
password |
Basic auth password |
LOGGING_LEVEL_COM_ANR |
INFO |
Application logging level |
JAVA_OPTS |
-Xmx512m -Xms256m |
JVM options |
Custom Configuration¶
Create a custom docker-compose.override.yml:
version: '3.8'
services:
app:
environment:
- SPRING_SECURITY_USER_PASSWORD=your-secure-password
- JAVA_OPTS=-Xmx1g -Xms512m
ports:
- "9090:8080"
Then run:
Docker Image Details¶
Base Image¶
- Build Stage:
eclipse-temurin:21-jdk-alpine - Runtime Stage:
eclipse-temurin:21-jre-alpine
Features¶
- ✅ Multi-stage build for smaller image size
- ✅ Non-root user for security
- ✅ Health checks configured
- ✅ Optimized JVM settings for containers
- ✅ Java 21 virtual threads enabled
- ✅ Layer caching for faster builds
Image Size¶
- Approximate size: ~300MB (runtime image)
Health Checks¶
Application Health¶
Expected response:
{
"status": "UP",
"components": {
"circuitBreakers": {
"status": "UP"
},
"diskSpace": {
"status": "UP"
},
"mongo": {
"status": "UP"
},
"ping": {
"status": "UP"
}
}
}
MongoDB Health¶
Troubleshooting¶
Application won't start¶
-
Check logs:
-
Verify MongoDB is running:
-
Check MongoDB connectivity:
MongoDB connection issues¶
-
Verify MongoDB is healthy:
-
Check network connectivity:
Port already in use¶
If port 8080 or 27017 is already in use, modify docker-compose.yml:
services:
app:
ports:
- "9090:8080" # Change host port
mongodb:
ports:
- "27018:27017" # Change host port
Out of memory¶
Increase memory limits in docker-compose.yml:
Production Deployment¶
Security Recommendations¶
-
Change default credentials:
-
Use secrets management:
-
Enable HTTPS:
- Use a reverse proxy (nginx, traefik)
-
Configure SSL certificates
-
Restrict network access:
Performance Tuning¶
-
Adjust JVM heap:
-
Enable JVM optimizations:
-
Scale horizontally:
Monitoring¶
View Resource Usage¶
View Application Metrics¶
Export Logs¶
Cleanup¶
Remove containers and networks¶
Remove containers, networks, and volumes¶
Remove images¶
Complete cleanup¶
Development Workflow¶
Rebuild after code changes¶
Run tests before building¶
Debug mode¶
Additional Resources¶
Last Updated: October 17, 2025
Docker Version: 20.10+
Docker Compose Version: 2.0+