1+ # Builder Stage
2+ FROM python:3.11.4-slim-buster AS builder
3+
4+ # Set work directory
5+ WORKDIR /usr/src/app
6+
7+ # Set environment variables
8+ ENV PYTHONDONTWRITEBYTECODE 1
9+ ENV PYTHONUNBUFFERED 1
10+
11+ # Install system dependencies and GDAL
12+ RUN apt-get update && apt-get install -y netcat \
13+ gcc \
14+ python3-dev \
15+ postgresql-client \
16+ libpq-dev \
17+ gdal-bin \
18+ libgdal-dev \
19+ python3-gdal \
20+ && rm -rf /var/lib/apt/lists/*
21+
22+ # Set GDAL environment variables
23+ ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
24+ ENV C_INCLUDE_PATH=/usr/include/gdal
25+
26+ # Install Python dependencies
27+ COPY requirements.txt .
28+ RUN pip install --upgrade pip
29+ RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
30+
31+ # Final Stage
32+ FROM python:3.11.4-slim-buster
33+
34+ # Create app user
35+ RUN addgroup --system app && adduser --system --group app
36+
37+ # Create app directories
38+ ENV HOME=/home/app
39+ ENV APP_HOME=/home/app/web
40+ RUN mkdir -p $APP_HOME
41+ RUN mkdir $APP_HOME/staticfiles
42+ RUN mkdir $APP_HOME/mediafiles
43+ WORKDIR $APP_HOME
44+
45+ # Install system dependencies
46+ RUN apt-get update && apt-get install -y \
47+ postgresql-client \
48+ gdal-bin \
49+ libgdal-dev \
50+ python3-gdal \
51+ netcat \
52+ && rm -rf /var/lib/apt/lists/*
53+
54+ # Set GDAL environment variables
55+ ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
56+ ENV C_INCLUDE_PATH=/usr/include/gdal
57+
58+ # Install Python packages
59+ COPY --from=builder /usr/src/app/wheels /wheels
60+ COPY --from=builder /usr/src/app/requirements.txt .
61+ RUN pip install --no-cache-dir --upgrade pip
62+ RUN pip install --no-cache-dir /wheels/*
63+
64+ # Copy project files
65+ COPY . $APP_HOME
66+
67+ # Ensure the dev-data directory has correct permissions
68+ RUN chown -R app:app $APP_HOME/dev-data
69+
70+ # Copy and set entrypoint
71+ COPY ./entrypoint.prod.sh .
72+ RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh
73+ RUN chmod +x $APP_HOME/entrypoint.prod.sh
74+
75+ # Set permissions
76+ RUN chown -R app:app $APP_HOME
77+
78+ # Switch to app user
79+ USER app
80+
81+ # Run entrypoint script
82+ ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]
83+ CMD ["gunicorn", "--bind", "0.0.0.0:8000", "Natours_Django.wsgi:application"]
0 commit comments