Overview
Portfolio Hub API uses a multi-stage Docker build for optimal performance and security. The application is containerized with MySQL database support via Docker Compose.The multi-stage build reduces the final image size by excluding build tools and only including the runtime environment.
Prerequisites
Before deploying with Docker, ensure you have:Install Docker
Download and install Docker Desktop for your operating system.
Install Docker Compose
Docker Compose is included with Docker Desktop. For Linux, install it separately:
Dockerfile Architecture
The Portfolio Hub API uses a multi-stage Docker build with two stages:Stage 1: Build Stage
This stage compiles the Spring Boot application using Maven:Why use dependency caching?
Why use dependency caching?
By copying
pom.xml first and running mvn dependency:go-offline, Docker caches the dependencies layer. This means subsequent builds only re-download dependencies if pom.xml changes, significantly speeding up build times.Stage 2: Production Stage
This stage creates a minimal runtime image:Building the Docker Image
Build Manually
To build the Docker image manually:Build with custom port
Build with custom port
Verify the Image
Check that the image was created successfully:Running with Docker Compose
The recommended way to run Portfolio Hub API is using Docker Compose, which orchestrates both the application and MySQL database.Docker Compose Configuration
The MySQL container exposes port 3307 on the host to avoid conflicts with existing MySQL installations using the default port 3306.
Environment File Setup
Create a.env file in the project root with all required environment variables:
.env
Start the Services
Access the Application
Once started, access:- API:
http://localhost:8080 - Swagger UI:
http://localhost:8080/swagger-ui/index.html - MySQL:
localhost:3307(from host machine)
Docker Compose Commands
Running Container Manually
If you prefer to run the container without Docker Compose:1. Create Docker Network
2. Run MySQL Container
3. Run API Container
Troubleshooting
Container fails to start
Container fails to start
Check the container logs:Common issues:
- Missing environment variables
- Database connection errors
- Port conflicts
Database connection refused
Database connection refused
Ensure the MySQL container is running:Verify the
MYSQL_HOST is set to mysql-db (the service name) in your .env file.Port already in use
Port already in use
If port 8080 or 3307 is already in use, modify the ports in
docker-compose.yml:Flyway migration errors
Flyway migration errors
The application uses Flyway for database migrations. If you see migration errors:
- Check that the database schema exists
- Verify Flyway baseline settings
- Clear the database and restart:
Production Considerations
Use External Database
For production, use a managed database service (AWS RDS, Google Cloud SQL) instead of a containerized MySQL instance.
Enable Health Checks
Add health check endpoints and configure Docker health checks in your compose file.
Use Secrets Management
Use Docker Secrets or external secret managers (AWS Secrets Manager, HashiCorp Vault) instead of
.env files.Enable Monitoring
Integrate monitoring tools like Prometheus and Grafana to track container metrics.
Next Steps
Environment Variables
Learn about all configuration options
API Reference
Explore available endpoints