File size: 4,001 Bytes
2df0cf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{% if config.multiStage %}
# Multi-stage build for Python {{ config.pythonVersion or '3.11' }}
FROM python:{{ config.pythonVersion or '3.11' }}-slim AS builder

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements
COPY requirements.txt .

# Install Python dependencies to /opt/venv
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir -r requirements.txt

# Runtime stage
FROM python:{{ config.pythonVersion or '3.11' }}-slim

# Set environment variables
ENV PATH="/opt/venv/bin:$PATH" \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /app

# Install runtime dependencies only
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy venv from builder
COPY --from=builder /opt/venv /opt/venv

# Copy application code
COPY . .

# Create non-root user
RUN useradd --create-home --shell /bin/bash --uid 1000 app && \
    chown -R app:app /app

USER app

# Expose port
EXPOSE {{ config.port or '8000' }}

# Production mode
{% if config.framework == 'fastapi' or config.framework == 'uvicorn' %}
CMD ["uvicorn", "{{ config.entrypoint or 'app.main:app' }}", "--host", "0.0.0.0", "--port", "{{ config.port or '8000' }}"]
{% elif config.framework == 'django' %}
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:{{ config.port or '8000' }}"]
{% elif config.framework == 'flask' %}
CMD ["gunicorn", "{{ config.entrypoint or 'app:app' }}", "--bind", "0.0.0.0:{{ config.port or '8000' }}"]
{% else %}
CMD ["python", "{{ config.entrypoint or 'app.py' }}"]
{% endif %}

{% else %}
# Single-stage Python {{ config.pythonVersion or '3.11' }} Dockerfile
FROM python:{{ config.pythonVersion or '3.11' }}-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements
COPY requirements.txt .

{% if config.developmentMode %}
# Development mode - install all dependencies
RUN pip install --no-cache-dir -r requirements.txt
{% else %}
# Production mode - use venv for cleaner dependencies
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir -r requirements.txt
{% endif %}

# Copy application code
COPY . .

# Create non-root user
RUN useradd --create-home --shell /bin/bash --uid 1000 app && \
    chown -R app:app /app

USER app

# Expose port
EXPOSE {{ config.port or '8000' }}

{% if config.developmentMode %}
# Development mode
ENV PYTHONUNBUFFERED=1

{% if config.framework == 'fastapi' or config.framework == 'uvicorn' %}
# FastAPI development server with auto-reload
CMD ["uvicorn", "{{ config.entrypoint or 'app.main:app' }}", "--host", "0.0.0.0", "--port", "{{ config.port or '8000' }}", "--reload"]
{% elif config.framework == 'django' %}
# Django development server
CMD ["python", "manage.py", "runserver", "0.0.0.0:{{ config.port or '8000' }}"]
{% elif config.framework == 'flask' %}
# Flask development server
CMD ["flask", "run", "--host=0.0.0.0", "--port={{ config.port or '8000' }}"]
{% else %}
# Custom Python application
CMD ["python", "{{ config.entrypoint or 'app.py' }}"]
{% endif %}

{% else %}
# Production mode
{% if config.framework == 'fastapi' or config.framework == 'uvicorn' %}
CMD ["uvicorn", "{{ config.entrypoint or 'app.main:app' }}", "--host", "0.0.0.0", "--port", "{{ config.port or '8000' }}"]
{% elif config.framework == 'django' %}
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:{{ config.port or '8000' }}"]
{% elif config.framework == 'flask' %}
CMD ["gunicorn", "{{ config.entrypoint or 'app:app' }}", "--bind", "0.0.0.0:{{ config.port or '8000' }}"]
{% else %}
CMD ["python", "{{ config.entrypoint or 'app.py' }}"]
{% endif %}
{% endif %}

{% endif %}