Skip to main content
This guide covers building and running the FoodTech Kitchen Service using Docker, providing a consistent and isolated environment for deployment.

Prerequisites

Ensure you have Docker installed on your system:
  • Docker: Version 20.10 or higher
  • Docker Compose (optional): For orchestration
Verify installation:
docker --version

Docker Architecture

The project uses a multi-stage Dockerfile to optimize build times and minimize the final image size:
  1. Build Stage: Uses gradle:8.5-jdk17 to compile the application
  2. Runtime Stage: Uses eclipse-temurin:17-jre-alpine for a lightweight production image

Building the Docker Image

1

Navigate to Project Directory

Ensure you’re in the root directory of the project:
cd FoodTech
2

Build the Docker Image

Build the image using the provided Dockerfile:
docker build -t foodtech-kitchen:latest .
This process will:
  • Copy the project files into the build container
  • Run ./gradlew clean build -x test to 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.
3

Verify the Image

Check that the image was created successfully:
docker images | grep foodtech-kitchen
You should see an entry with the tag latest.

Running the Container

1

Run in Foreground

Start the container with output visible in your terminal:
docker run -p 8080:8080 foodtech-kitchen:latest
The application logs will be displayed in real-time. Press Ctrl+C to stop.
2

Run in Background (Detached Mode)

For production-like deployment, run the container in detached mode:
docker run -d -p 8080:8080 --name foodtech-api foodtech-kitchen:latest
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-api for easy reference
3

Verify the Container is Running

Check the container status:
docker ps
You should see foodtech-api in the list of running containers.
4

Test the API

Send a request to verify the application is responding:
curl -X POST http://localhost:8080/api/orders \
  -H "Content-Type: application/json" \
  -d '{
    "tableNumber": "A1",
    "products": [
      {"name": "Coca Cola", "type": "DRINK"}
    ]
  }'

Managing the Container

View Container Logs

docker logs -f foodtech-api

Stop the Container

docker stop foodtech-api

Start a Stopped Container

docker start foodtech-api

Remove the Container

docker rm foodtech-api

Dockerfile Explained

Here’s a breakdown of the multi-stage Dockerfile:
# Build stage - Uses full JDK and Gradle for compilation
FROM gradle:8.5-jdk17 AS build
WORKDIR /app

# Copy build configuration files
COPY build.gradle settings.gradle gradlew ./
COPY gradle ./gradle

# Copy source code
COPY src ./src

# Build the application (skip tests for faster builds)
RUN ./gradlew clean build -x test

# Runtime stage - Uses minimal JRE Alpine for smaller image
FROM eclipse-temurin:17-jre-alpine
WORKDIR /app

# Copy only the JAR file from build stage
COPY --from=build /app/build/libs/*.jar app.jar

# Expose port 8080
EXPOSE 8080

# Run the application
ENTRYPOINT ["java", "-jar", "app.jar"]

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:
docker run -d -p 8080:8080 \
  -e SERVER_PORT=8080 \
  -e SPRING_PROFILES_ACTIVE=prod \
  --name foodtech-api \
  foodtech-kitchen:latest

Advanced Configuration

Custom Port Mapping

Map the container to a different host port:
docker run -d -p 9090:8080 --name foodtech-api foodtech-kitchen:latest
Access the API at http://localhost:9090.

Volume Mounting for Logs

Persist logs outside the container:
docker run -d -p 8080:8080 \
  -v $(pwd)/logs:/app/logs \
  --name foodtech-api \
  foodtech-kitchen:latest

Troubleshooting

Build Fails with “Permission Denied”

Ensure the gradlew file has execute permissions:
chmod +x gradlew
docker build -t foodtech-kitchen:latest .

Container Exits Immediately

Check the logs for errors:
docker logs foodtech-api

Port Already in Use

If port 8080 is occupied, use a different port mapping:
docker run -d -p 8081:8080 --name foodtech-api foodtech-kitchen:latest

Next Steps

Build docs developers (and LLMs) love