From 9540c0c5cbcf65baacebf0faa879fba42b56e097 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Sun, 21 Sep 2025 12:07:51 +0530 Subject: [PATCH 1/4] Create Dockerfile --- Dockerfile | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..330a4ff --- /dev/null +++ b/Dockerfile @@ -0,0 +1,54 @@ +# Multi-stage Dockerfile for Tech Stack Advisor ML App + +# Stage 1: Builder stage for training the model +FROM python:3.11-slim AS builder + +WORKDIR /app + +# Install build dependencies +RUN apt-get update && apt-get install -y \ + gcc \ + python3-dev \ + && rm -rf /var/lib/apt/lists/* + +# Copy requirements and install Python dependencies +COPY requirements.txt . +RUN pip install --no-cache-dir --user -r requirements.txt + +# Copy training script and train the model +COPY train.py . +RUN python train.py + +# Stage 2: Production runtime stage +FROM python:3.11-slim AS production + +# Create non-root user for security +RUN useradd --create-home --shell /bin/bash mluser + +WORKDIR /app + +# Copy Python packages from builder stage +COPY --from=builder /root/.local /home/mluser/.local + +# Copy application files +COPY app.py . +COPY requirements.txt . + +# Copy trained model from builder stage +COPY --from=builder /app/model.pkl . +COPY --from=builder /app/encoders.pkl . + +# Set ownership and switch to non-root user +RUN chown -R mluser:mluser /app +USER mluser + +# Make sure scripts in .local are usable +ENV PATH=/home/mluser/.local/bin:$PATH + +# Add health check +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ + CMD python -c "import requests; requests.get('http://localhost:7860', timeout=3)" || exit 1 + +EXPOSE 7860 + +CMD ["python", "app.py"] From 2d4f3ecd6a86b362635bc0bb4688ec47d5b2ebdc Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Sun, 21 Sep 2025 12:32:13 +0530 Subject: [PATCH 2/4] Update goose.yml gemma2-9b-it --- .github/workflows/goose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/goose.yml b/.github/workflows/goose.yml index 9b59a66..352cbf0 100644 --- a/.github/workflows/goose.yml +++ b/.github/workflows/goose.yml @@ -48,7 +48,7 @@ jobs: mkdir -p ~/.config/goose cat > ~/.config/goose/config.yaml <<'EOF' GOOSE_PROVIDER: groq - GOOSE_MODEL: llama-3.3-70b-versatile + GOOSE_MODEL: gemma2-9b-it keyring: false EOF From 48a14b610f59529f656f3758f0e4fff55f52e1c9 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Sun, 21 Sep 2025 12:33:31 +0530 Subject: [PATCH 3/4] Update goose.yml switch to llama-3.3-70b-versatile --- .github/workflows/goose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/goose.yml b/.github/workflows/goose.yml index 352cbf0..9b59a66 100644 --- a/.github/workflows/goose.yml +++ b/.github/workflows/goose.yml @@ -48,7 +48,7 @@ jobs: mkdir -p ~/.config/goose cat > ~/.config/goose/config.yaml <<'EOF' GOOSE_PROVIDER: groq - GOOSE_MODEL: gemma2-9b-it + GOOSE_MODEL: llama-3.3-70b-versatile keyring: false EOF From c0385dc0c9da502c796c2ed508be0ec323ff4968 Mon Sep 17 00:00:00 2001 From: Gourav Shah Date: Sun, 21 Sep 2025 12:38:34 +0530 Subject: [PATCH 4/4] Update Dockerfile BuildKit optimized Dockerfile with advanced caching and multi-arch support --- Dockerfile | 83 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 330a4ff..a6d81a0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,54 +1,89 @@ -# Multi-stage Dockerfile for Tech Stack Advisor ML App +# syntax=docker/dockerfile:1 +# BuildKit optimized Dockerfile with advanced caching and multi-arch support -# Stage 1: Builder stage for training the model -FROM python:3.11-slim AS builder +ARG TARGETPLATFORM +ARG BUILDPLATFORM +ARG TARGETOS +ARG TARGETARCH + +# Stage 1: Dependencies stage with cache mounts +FROM --platform=$BUILDPLATFORM python:3.11-slim AS dependencies WORKDIR /app -# Install build dependencies -RUN apt-get update && apt-get install -y \ +# Use cache mount for apt packages +RUN --mount=type=cache,target=/var/cache/apt \ + --mount=type=cache,target=/var/lib/apt \ + apt-get update && apt-get install -y \ gcc \ - python3-dev \ - && rm -rf /var/lib/apt/lists/* + python3-dev -# Copy requirements and install Python dependencies +# Use cache mount for pip packages COPY requirements.txt . -RUN pip install --no-cache-dir --user -r requirements.txt +RUN --mount=type=cache,target=/root/.cache/pip \ + pip install --user -r requirements.txt + +# Stage 2: Model training stage +FROM dependencies AS trainer -# Copy training script and train the model COPY train.py . -RUN python train.py -# Stage 2: Production runtime stage -FROM python:3.11-slim AS production +# Train the model with cache mount for any downloads +RUN --mount=type=cache,target=/tmp \ + python train.py + +# Stage 3: Final production stage +FROM --platform=$TARGETPLATFORM python:3.11-slim AS production + +# Display build info for debugging +RUN echo "Building for platform: $TARGETPLATFORM, architecture: $TARGETARCH" -# Create non-root user for security +# Create non-root user RUN useradd --create-home --shell /bin/bash mluser WORKDIR /app -# Copy Python packages from builder stage -COPY --from=builder /root/.local /home/mluser/.local +# Copy Python packages from dependencies stage +COPY --from=dependencies /root/.local /home/mluser/.local # Copy application files COPY app.py . COPY requirements.txt . -# Copy trained model from builder stage -COPY --from=builder /app/model.pkl . -COPY --from=builder /app/encoders.pkl . +# Copy trained models from trainer stage +COPY --from=trainer /app/*.pkl ./ -# Set ownership and switch to non-root user +# Set proper ownership RUN chown -R mluser:mluser /app USER mluser -# Make sure scripts in .local are usable +# Update PATH for user-installed packages ENV PATH=/home/mluser/.local/bin:$PATH -# Add health check -HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ - CMD python -c "import requests; requests.get('http://localhost:7860', timeout=3)" || exit 1 +# Health check with improved reliability +HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860', timeout=5)" || exit 1 EXPOSE 7860 CMD ["python", "app.py"] +``` + +### Step 3: Build for Multiple Platforms + +``` +# Build for single platform and load locally +docker buildx build \ + --platform linux/amd64 \ + -t tech-stack-advisor:multiarch \ + --load . + +# Login to Registrt to be able to push image +docker login + +# For multiple architectures (requires registry push) +# replace xxxxxx with your Dockerhub Username or equivalent +docker buildx build \ + --platform linux/amd64,linux/arm64 \ + -t xxxxxx/tech-stack-advisor:multiarch \ + --push .