Dockerfile
The project uses a multi-stage build to optimize the final image size:Build Process Explained
Stage 1: Build Stage
- Base Image: Uses
maven:3.9-eclipse-temurin-21which includes Maven and JDK 21 - Dependency Caching: Copies
pom.xmlfirst and downloads dependencies separately to leverage Docker layer caching - Build Application: Copies source code and packages the application with
mvn package - Skip Tests: Uses
-DskipTeststo speed up the build (tests should run in CI/CD)
Stage 2: Runtime Stage
- Base Image: Uses
eclipse-temurin:21-jre(JRE only, much smaller than JDK) - Copy Artifact: Copies only the built JAR from the build stage
- Expose Port: Documents that the application listens on port 8080
- Entry Point: Runs the JAR file when the container starts
Building the Docker Image
Build the image from the project root:Running the Docker Container
Prerequisites
The application requires MongoDB and MySQL to be running. You can run them separately:Run the API Container
Environment Variables
The application supports multiple Spring profiles:local- For local developmentdocker- For Docker deployment (uses service names as hostnames)prod- For production deployment (uses environment variables)
Verify the Container
Check if the container is running:Using Pre-built Image from Docker Hub
The official image is available on Docker Hub:Docker Commands Reference
Stop the Container
Remove the Container
Remove the Image
Inspect Container
Execute Commands in Running Container
Best Practices
.dockerignore
The project includes a.dockerignore file to exclude unnecessary files from the build context:
Health Checks
Add a health check to the Dockerfile (optional):Resource Limits
Run with resource limits:Testing the API
Once the container is running, test the API:Next Steps
- Docker Compose - Run the entire stack with one command
- Cloud Deployment - Deploy to production cloud platforms