Create Dockerfile
Browse files- Dockerfile +49 -0
Dockerfile
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use Python 3.11 slim image for smaller size and security
|
2 |
+
FROM python:3.11-slim
|
3 |
+
|
4 |
+
# Set environment variables
|
5 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
6 |
+
PYTHONUNBUFFERED=1 \
|
7 |
+
PYTHONPATH=/app \
|
8 |
+
PORT=8000
|
9 |
+
|
10 |
+
# Create a non-root user for security
|
11 |
+
RUN groupadd --gid 1000 appuser && \
|
12 |
+
useradd --uid 1000 --gid appuser --shell /bin/bash --create-home appuser
|
13 |
+
|
14 |
+
# Set working directory
|
15 |
+
WORKDIR /app
|
16 |
+
|
17 |
+
# Install system dependencies required for curl-cffi and other packages
|
18 |
+
RUN apt-get update && apt-get install -y \
|
19 |
+
--no-install-recommends \
|
20 |
+
build-essential \
|
21 |
+
curl \
|
22 |
+
&& rm -rf /var/lib/apt/lists/* \
|
23 |
+
&& apt-get clean
|
24 |
+
|
25 |
+
# Copy requirements first for better Docker layer caching
|
26 |
+
COPY requirements.txt .
|
27 |
+
|
28 |
+
# Install Python dependencies
|
29 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
30 |
+
pip install --no-cache-dir -r requirements.txt
|
31 |
+
|
32 |
+
# Copy application code
|
33 |
+
COPY main.py .
|
34 |
+
|
35 |
+
# Change ownership of the app directory to the non-root user
|
36 |
+
RUN chown -R appuser:appuser /app
|
37 |
+
|
38 |
+
# Switch to non-root user
|
39 |
+
USER appuser
|
40 |
+
|
41 |
+
# Expose the port the app runs on
|
42 |
+
EXPOSE 8000
|
43 |
+
|
44 |
+
# Add health check
|
45 |
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
46 |
+
CMD curl -f http://localhost:8000/health || exit 1
|
47 |
+
|
48 |
+
# Command to run the application
|
49 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
|