# Use an official Python runtime as the base image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

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

# Set environment variables
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0

# Run app.py when the container launches
CMD ["flask", "run"]

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Create a non-root user and switch to it
RUN adduser --disabled-password --gecos '' myuser
USER myuser

# Health check to verify the application is running
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:5000/ || exit 1

# Add labels for better image management
LABEL maintainer="example@example.com"
LABEL version="1.0"
LABEL description="This is an example Dockerfile for a Python Flask application."

# Multi-stage build example (commented out)
# FROM python:3.9-slim AS builder
# WORKDIR /app
# COPY requirements.txt .
# RUN pip install --user -r requirements.txt
# 
# FROM python:3.9-slim
# WORKDIR /app
# COPY --from=builder /root/.local /root/.local
# COPY . .
# ENV PATH=/root/.local/bin:$PATH
# CMD ["python", "app.py"]