Overview
The TelegrmBot API uses a multi-stage Docker build process with Docker Compose for orchestrating the application and PostgreSQL database. This approach ensures consistent deployments across all environments.Prerequisites
Before deploying with Docker, ensure you have:- Docker Engine 20.10 or higher
- Docker Compose V2
- At least 2GB of available RAM
- Telegram Bot Token from @BotFather
- OpenRouter API Key from openrouter.ai
Docker Architecture
Multi-Stage Build
The Dockerfile uses a two-stage build process for optimal image size and security:The build stage uses Maven 3.9.6 with JDK 21, while the runtime stage only includes JRE 21, reducing the final image size by ~300MB.
Docker Compose Services
Thedocker-compose.yml defines two services:
- db - PostgreSQL 15 database with health checks
- app - Spring Boot application that depends on the database
The app service waits for the database health check to pass before starting, preventing connection errors during initialization.
Deployment Steps
Configure environment variables
Copy the example environment file and configure your variables:Edit the
.env file with your actual credentials:Build and start containers
Use Docker Compose to build the application image and start all services:This command will:
- Build the Spring Boot application using Maven
- Create the Docker image with JRE 21
- Start the PostgreSQL database
- Wait for database health check
- Start the application container
The
--build flag forces a rebuild of the application image. Omit it for faster startups if no code changes were made.Test the API
Access the Swagger UI to verify the API is responding:Or open in your browser: http://localhost:8080/swagger-ui.html
Managing Containers
Viewing Logs
View logs from both services:Stopping Containers
Restarting Services
Health Checks
Database Health Check
The PostgreSQL container includes a built-in health check:starting, healthy, unhealthy
Application Health Check
Spring Boot Actuator provides health endpoints:Manual Container Inspection
Access the application container shell:Troubleshooting
Container Won’t Start
Database Connection Issues
If the app can’t connect to the database, ensure the database health check passed:The
db service should show healthy status.-
Restart the database service:
-
Check database logs for errors:
-
Verify database credentials in
.env
Application Build Fails
If Maven build fails during Docker image creation:-
Check Maven output:
-
Test build locally:
-
Clear Docker build cache:
Out of Memory Errors
Increase Docker memory limits in Docker Desktop settings or use:Production Considerations
Security Hardening
-
Use Docker secrets instead of environment variables:
-
Limit container resources:
- Run behind a reverse proxy (Nginx/Traefik)
- Enable HTTPS with SSL certificates
- Use read-only filesystem where possible
Database Backups
Create automated backups:Monitoring and Logging
- Configure centralized logging (ELK, Splunk, etc.)
- Set up container monitoring (Prometheus, Grafana)
- Enable Spring Boot Actuator metrics:
Advanced Configuration
Custom Network Configuration
Create a custom Docker network:External Database
To use an external PostgreSQL instance instead of the Docker database:- Remove the
dbservice fromdocker-compose.yml - Update the
DB_URLin.env:
Multiple Environments
Create environment-specific compose files:Next Steps
Environment Variables
Complete reference of all configuration options
Local Development
Run the application without Docker for development