From 7ba05422f057b01ab97316e8e172e8390b6470fc Mon Sep 17 00:00:00 2001 From: Francisco Prin Date: Tue, 9 Sep 2025 16:11:03 -0600 Subject: [PATCH] feat: add Docker configuration for containerized deployment with Python 2.7 --- .dockerignore | 25 +++++++++++++++++++++++++ Dockerfile | 39 +++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 10 ++++++++++ entrypoint.sh | 20 ++++++++++++++++++++ requirements.txt | 2 +- 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..39509c7a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +# Python +__pycache__/ +*.py[cod] +*.egg-info/ + +# VCS +.git/ +.gitignore + +# IDE +.vscode/ +.idea/ + +# Envs +.env +.venv/ +venv/ + +# OS files +.DS_Store + +# Local DB and caches +*.sqlite3 +/db.sqlite3 +staticfiles/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..3eea0980 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# Base image with Python 2.7 +FROM python:2.7-slim + +# Prevent pyc files and enable unbuffered logs +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +# System dependencies for building wheels and required libs (psycopg2, pylibmc, etc.) +# Note: Debian buster is EOL. Point sources to archive.debian.org and disable validity check. +RUN sed -i -e 's|deb.debian.org|archive.debian.org|g' \ + -e 's|security.debian.org/debian-security|archive.debian.org/debian-security|g' /etc/apt/sources.list \ + && apt-get -o Acquire::Check-Valid-Until=false update \ + && apt-get install -y --no-install-recommends \ + build-essential \ + gcc \ + libpq-dev \ + python-dev \ + libmemcached-dev \ + libsasl2-dev \ + zlib1g-dev \ + && rm -rf /var/lib/apt/lists/* + +# Install dependencies first for better caching +COPY requirements.txt /app/ +RUN pip install --no-cache-dir -r requirements.txt + +# Copy project files +COPY . /app + +# Ensure entrypoint is executable +RUN chmod +x entrypoint.sh + +# Expose gunicorn port +EXPOSE 8000 + +# Default command +CMD ["./entrypoint.sh"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..f6db1948 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3.8" +services: + web: + build: + context: . + dockerfile: Dockerfile + ports: + - "8001:8000" + environment: + - DEBUG=1 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..42f837f0 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Apply database migrations +python manage.py migrate --noinput + +# Load initial data fixtures (mirrors Makefile load_data target) +python manage.py loaddata planets.json +python manage.py loaddata people.json +python manage.py loaddata species.json +python manage.py loaddata transport.json +python manage.py loaddata starships.json +python manage.py loaddata vehicles.json +python manage.py loaddata films.json + +# Collect static files +python manage.py collectstatic --noinput + +# Start server +exec gunicorn swapi.wsgi --bind 0.0.0.0:8000 --log-file - diff --git a/requirements.txt b/requirements.txt index c389e7bd..640fff5b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,7 +18,7 @@ drf-json-api==0.1.0 gunicorn==19.1.1 keen==0.3.7 markdown2==2.3.0 -psycopg2==2.5.4 +psycopg2==2.8.6 pycrypto==2.6.1 pylibmc==1.4.1 requests==2.5.1