From a325f8cd99152ff554af616cc4b600ee3d46b1b0 Mon Sep 17 00:00:00 2001 From: Sk_Akib_Ahammed Date: Sat, 14 Feb 2026 16:21:21 +0530 Subject: [PATCH] backend is ready for deployment --- Dockerfile | 94 +++++++++++++++++++++++++++------------------------ app/v1/app.py | 2 +- compose.yaml | 2 +- main.py | 2 +- 4 files changed, 52 insertions(+), 48 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6f58413..4f2bce8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,54 +1,58 @@ -#Dockerfile - # syntax=docker/dockerfile:1 -# Comments are provided throughout this file to help you get started. -# If you need more help, visit the Dockerfile reference guide at -# https://docs.docker.com/go/dockerfile-reference/ - -# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 - -ARG PYTHON_VERSION=3.13.1 -FROM python:${PYTHON_VERSION}-slim AS base - -# Prevents Python from writing pyc files. -ENV PYTHONDONTWRITEBYTECODE=1 +############################ +# 1️⃣ Base Image +############################ +FROM python:3.12-slim AS base -# Keeps Python from buffering stdout and stderr to avoid situations where -# the application crashes without emitting any logs due to buffering. -ENV PYTHONUNBUFFERED=1 +# Environment settings +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PORT=8080 WORKDIR /app -# Create a non-privileged user that the app will run under. -# See https://docs.docker.com/go/dockerfile-user-best-practices/ -ARG UID=10001 -RUN adduser \ - --disabled-password \ - --gecos "" \ - --home "/nonexistent" \ - --shell "/sbin/nologin" \ - --no-create-home \ - --uid "${UID}" \ - appuser - -# Download dependencies as a separate step to take advantage of Docker's caching. -# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds. -# Leverage a bind mount to requirements.txt to avoid having to copy them into -# into this layer. -RUN --mount=type=cache,target=/root/.cache/pip \ - --mount=type=bind,source=requirements.txt,target=requirements.txt \ - python -m pip install -r requirements.txt - -# Switch to the non-privileged user to run the application. -USER appuser - -# Copy the source code into the container. +############################ +# 2️⃣ System Dependencies +############################ +RUN apt-get update && apt-get install -y \ + build-essential \ + curl \ + && rm -rf /var/lib/apt/lists/* + +############################ +# 3️⃣ Install Python Dependencies +############################ +COPY requirements.txt . + +RUN pip install --upgrade pip \ + && pip install --no-cache-dir -r requirements.txt + +############################ +# 4️⃣ Create Non-Root User +############################ +RUN addgroup --system appgroup && \ + adduser --system --ingroup appgroup appuser + +############################ +# 5️⃣ Copy Application Code +############################ COPY . . -# Expose the port that the application listens on. -EXPOSE 8000 - -# Run the application. -CMD ["uvicorn", "app.v1.app:app", "--host", "0.0.0.0", "--port", "8000"] +############################ +# 6️⃣ Switch to Non-Root User +############################ +USER appuser +############################ +# 7️⃣ Expose Cloud Run Port +############################ +EXPOSE 8080 + +############################ +# 8️⃣ Production Server Command +############################ +CMD ["uvicorn", "app.v1.app:app", \ + "--host", "0.0.0.0", \ + "--port", "8080", \ + "--workers", "2"] diff --git a/app/v1/app.py b/app/v1/app.py index 0ee231f..0ddb1c8 100644 --- a/app/v1/app.py +++ b/app/v1/app.py @@ -51,7 +51,7 @@ def rate_limit_key(request: Request): limiter = Limiter(key_func=rate_limit_key) -app = FastAPI() +app = FastAPI(docs_url=None, redoc_url=None) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) diff --git a/compose.yaml b/compose.yaml index 731f965..8ee6333 100644 --- a/compose.yaml +++ b/compose.yaml @@ -14,7 +14,7 @@ services: build: context: . ports: - - "8000:8000" + - "8080:8080" env_file: - .env volumes: diff --git a/main.py b/main.py index 497310f..e0472a7 100644 --- a/main.py +++ b/main.py @@ -3,4 +3,4 @@ import uvicorn if __name__ == "__main__": - uvicorn.run("app.v1.app:app", host="0.0.0.0", port=8000, reload=True) + uvicorn.run("app.v1.app:app", host="0.0.0.0", port=8080)