-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDockerfile
More file actions
81 lines (60 loc) · 1.98 KB
/
Copy pathDockerfile
File metadata and controls
81 lines (60 loc) · 1.98 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Multi-stage Dockerfile for Open LPR Application
# Stage 1: Base stage with Python dependencies
FROM python:3.11-slim AS base
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
gosu \
fonts-noto \
fonts-noto-cjk \
libraqm0 \
&& rm -rf /var/lib/apt/lists/*
# Create and activate virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Upgrade pip
RUN pip install --upgrade pip
# Copy and install Python dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Verify gunicorn is installed
RUN which gunicorn || (echo "Gunicorn not found in PATH" && exit 1)
# Stage 2: Build stage for application code
FROM base AS builder
# Set work directory
WORKDIR /app
# Copy application code
COPY . .
# Create directories for volumes
RUN mkdir -p /app/data /app/media /app/staticfiles /app/metrics
# Collect static files
RUN python manage.py collectstatic --noinput
# Stage 3: Production stage
FROM base AS production
# Create non-root user
RUN groupadd -r django && useradd -r -g django django
# Set work directory
WORKDIR /app
# Create directories with proper permissions
RUN mkdir -p /app/data /app/media /app/staticfiles /app/metrics && \
chown -R django:django /app && \
chmod -R 755 /app/data /app/media /app/staticfiles /app/metrics
# Copy application from builder stage
COPY --from=builder --chown=django:django /app /app
# Ensure PATH is set correctly for the virtual environment
ENV PATH="/opt/venv/bin:$PATH"
# Switch to non-root user
USER django
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD python manage.py check || exit 1
# Default command
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "--timeout", "120", "lpr_project.wsgi:application"]