Prerequisites
Ensure you have Docker installed on your system:- Docker: Version 20.10 or higher
- Docker Compose (optional): For orchestration
Docker Architecture
The project uses a multi-stage Dockerfile to optimize build times and minimize the final image size:- Build Stage: Uses
gradle:8.5-jdk17to compile the application - Runtime Stage: Uses
eclipse-temurin:17-jre-alpinefor a lightweight production image
Building the Docker Image
Build the Docker Image
Build the image using the provided Dockerfile:This process will:
- Copy the project files into the build container
- Run
./gradlew clean build -x testto compile the application - Copy the resulting JAR file to a minimal Alpine Linux runtime image
- Configure the application to run on port 8080
The build stage excludes tests (
-x test) to speed up the Docker build. Run tests locally before building production images.Running the Container
Run in Foreground
Start the container with output visible in your terminal:The application logs will be displayed in real-time. Press
Ctrl+C to stop.Run in Background (Detached Mode)
For production-like deployment, run the container in detached mode:This command:
- Runs the container in the background (
-d) - Maps port 8080 on the host to port 8080 in the container (
-p 8080:8080) - Names the container
foodtech-apifor easy reference
Verify the Container is Running
Check the container status:You should see
foodtech-api in the list of running containers.Managing the Container
View Container Logs
Stop the Container
Start a Stopped Container
Remove the Container
Dockerfile Explained
Here’s a breakdown of the multi-stage Dockerfile:Image Size Optimization
The multi-stage build significantly reduces the final image size:- Build stage: ~1.2 GB (includes Gradle, JDK, and build dependencies)
- Runtime stage: ~200 MB (only JRE and application JAR)
Environment Variables
You can customize the application behavior using environment variables:Advanced Configuration
Custom Port Mapping
Map the container to a different host port:http://localhost:9090.
Volume Mounting for Logs
Persist logs outside the container:Troubleshooting
Build Fails with “Permission Denied”
Ensure thegradlew file has execute permissions:
Container Exits Immediately
Check the logs for errors:Port Already in Use
If port 8080 is occupied, use a different port mapping:Next Steps
- Learn about the Testing Strategy
- Review the API Reference
- Explore Architecture Overview