deploymate / app /templates /docker /dockerfile_python.j2
shakauthossain's picture
V2.0.0
2df0cf9 verified
{% 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 %}