-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDockerfile
More file actions
74 lines (55 loc) · 1.98 KB
/
Copy pathDockerfile
File metadata and controls
74 lines (55 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
# Multi-stage build for optimal image size
FROM python:3.12-slim as builder
# Set environment variables for Poetry
ENV POETRY_NO_INTERACTION=1 \
POETRY_VENV_IN_PROJECT=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache \
POETRY_HOME="/opt/poetry" \
POETRY_VERSION=1.8.3
# Install system dependencies needed for building
RUN apt-get update && apt-get install -y \
build-essential \
curl \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Install Poetry
RUN pip install poetry==$POETRY_VERSION
# Set working directory
WORKDIR /app
# Copy Poetry configuration files first (better layer caching)
COPY pyproject.toml poetry.lock ./
# Configure Poetry and install only production dependencies
RUN poetry config virtualenvs.create false \
&& poetry install --only=main --no-root \
&& rm -rf $POETRY_CACHE_DIR
# Production stage
FROM python:3.12-slim as production
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1
# Install only runtime dependencies
RUN apt-get update && apt-get install -y \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user for security
RUN groupadd -r appuser && useradd -r -g appuser appuser
# Set working directory
WORKDIR /app
# Copy installed packages from builder stage
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
# Copy application code (exclude unnecessary files via .dockerignore)
COPY . .
# Install the application using Poetry's project definition
RUN pip install -e . --no-deps
# Change ownership to non-root user
RUN chown -R appuser:appuser /app
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8080
# Add health check (requires adding health endpoint to your app)
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import httpx; httpx.get('http://localhost:8080/health', timeout=5)" || exit 1
# Run the application
CMD ["python", "-m", "app"]