From 55d08d36e05bde0d35d01f275b4d2974602ac468 Mon Sep 17 00:00:00 2001 From: mohamad Date: Sun, 1 Jun 2025 16:14:55 +0200 Subject: [PATCH] refactor: Revise .dockerignore and Dockerfile for enhanced build efficiency and organization - Updated .dockerignore to categorize ignored files, including logs and local development configurations. - Implemented a multi-stage build in Dockerfile to optimize image size and dependency management. - Added build dependencies and created a virtual environment for better isolation of Python packages. --- be/.dockerignore | 63 +++++++++++++++++++++++++++++++++--------------- be/Dockerfile | 50 ++++++++++++++++++++++---------------- 2 files changed, 73 insertions(+), 40 deletions(-) diff --git a/be/.dockerignore b/be/.dockerignore index 405b1e0..444af97 100644 --- a/be/.dockerignore +++ b/be/.dockerignore @@ -1,30 +1,55 @@ -# Git files +# Git .git .gitignore -# Virtual environment -.venv -venv/ -env/ -ENV/ -*.env # Ignore local .env files within the backend directory if any - -# Python cache +# Python __pycache__/ *.py[cod] *$py.class +*.so +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg -# IDE files +# Virtual Environment +venv/ +ENV/ + +# IDE .idea/ .vscode/ +*.swp +*.swo -# Test artifacts +# Logs +*.log + +# Local development +.env +.env.local +.env.*.local + +# Docker +Dockerfile* +docker-compose* +.dockerignore + +# Tests +tests/ +test/ .pytest_cache/ -htmlcov/ -.coverage* - -# Other build/temp files -*.egg-info/ -dist/ -build/ -*.db # e.g., sqlite temp dbs \ No newline at end of file +.coverage +htmlcov/ \ No newline at end of file diff --git a/be/Dockerfile b/be/Dockerfile index 5007905..83238b7 100644 --- a/be/Dockerfile +++ b/be/Dockerfile @@ -1,35 +1,43 @@ # be/Dockerfile -# Choose a suitable Python base image -FROM python:alpine +# Use multi-stage build +FROM python:alpine AS builder # Set environment variables -ENV PYTHONDONTWRITEBYTECODE 1 # Prevent python from writing pyc files -ENV PYTHONUNBUFFERED 1 # Keep stdout/stderr unbuffered +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=1 \ + PIP_DISABLE_PIP_VERSION_CHECK=1 -# Set the working directory in the container -WORKDIR /app +# Install build dependencies +RUN apk add --no-cache \ + gcc \ + musl-dev \ + postgresql-dev -# Install system dependencies if needed (e.g., for psycopg2 build) -# RUN apt-get update && apt-get install -y --no-install-recommends gcc build-essential libpq-dev && rm -rf /var/lib/apt/lists/* +# Create and activate virtual environment +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" -# Install Python dependencies -# Upgrade pip first -RUN pip install --no-cache-dir --upgrade pip -# Copy only requirements first to leverage Docker cache -COPY requirements.txt requirements.txt # Install dependencies +COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -# Copy the rest of the application code into the working directory -COPY . . -# This includes your 'app/' directory, alembic.ini, etc. +# Final stage +FROM python:alpine -# Expose the port the app runs on +# Copy virtual environment from builder +COPY --from=builder /opt/venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" + +# Set working directory +WORKDIR /app + +# Copy application code +COPY . . + +# Expose port EXPOSE 8000 -# Command to run the application using uvicorn -# The default command for production (can be overridden in docker-compose for development) -# Note: Make sure 'app.main:app' correctly points to your FastAPI app instance -# relative to the WORKDIR (/app). If your main.py is directly in /app, this is correct. +# Run the application CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file -- 2.45.2