-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
59 lines (44 loc) · 1.55 KB
/
Dockerfile
File metadata and controls
59 lines (44 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Stage 1: Build dependencies
FROM python:3.12-slim AS builder
# Prevent Python from writing pyc files and buffer output
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Install system dependencies for building Python packages
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
make \
libffi-dev \
libpq-dev \
build-essential \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 - --version 1.6.1
ENV PATH="/root/.local/bin:$PATH"
WORKDIR /app
# Copy only dependency files to leverage Docker layer caching
COPY pyproject.toml poetry.lock* /app/
# Install Python dependencies
RUN poetry config virtualenvs.create false \
&& poetry install --no-root --no-interaction --no-ansi
# Copy project source code
COPY . /app/
# Collect static files (optional, can also be run at runtime)
RUN python manage.py collectstatic --noinput
# Stage 2: Production image
FROM python:3.12-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Install runtime dependencies only
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
libffi7 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Copy installed packages from builder
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /app /app
# Expose Django port
EXPOSE 8000
# Run migrations and start Gunicorn
CMD ["sh", "-c", "python manage.py migrate && gunicorn core.wsgi:application --bind 0.0.0.0:8000 --workers 3"]