-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
111 lines (86 loc) · 3.14 KB
/
Dockerfile
File metadata and controls
111 lines (86 loc) · 3.14 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
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
# Multi-stage Dockerfile for py_template
# This provides a containerized development and testing environment
# ============================================================================
# Stage 1: Builder - Install dependencies
# ============================================================================
FROM python:3.12-slim AS builder
WORKDIR /app
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
# Copy dependency files
COPY pyproject.toml uv.lock* ./
COPY src/py_template/__version__.py ./src/py_template/__version__.py
# Create virtual environment and install dependencies
# Use --frozen to ensure reproducible builds
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-dev
# ============================================================================
# Stage 2: Development - For development and testing
# ============================================================================
FROM python:3.12-slim AS development
WORKDIR /app
# Copy uv from builder
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
# Copy dependency files
COPY pyproject.toml uv.lock* ./
COPY src/py_template/__version__.py ./src/py_template/__version__.py
# Install all dependencies including dev
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --extra dev
# Copy source code
COPY . .
# Set Python path
ENV PYTHONPATH=/app/src
ENV PATH="/app/.venv/bin:$PATH"
# Default command: run tests
CMD ["pytest", "-v"]
# ============================================================================
# Stage 3: Runtime - Minimal runtime for using the library
# ============================================================================
FROM python:3.12-slim AS runtime
WORKDIR /app
# Copy virtual environment from builder
COPY --from=builder /app/.venv /app/.venv
# Copy source code
COPY src/ ./src/
# Set environment variables
ENV PYTHONPATH=/app/src
ENV PATH="/app/.venv/bin:$PATH"
# Create non-root user
RUN useradd -m -u 1000 pytemplate && \
chown -R pytemplate:pytemplate /app
USER pytemplate
# Default command: show info
CMD ["py_template", "info"]
# ============================================================================
# Stage 4: Testing - For CI/CD
# ============================================================================
FROM development AS testing
# Copy test files
COPY tests/ ./tests/
# Run tests with coverage
CMD ["pytest", "--cov=src", "--cov-report=xml", "--cov-report=term"]
# ============================================================================
# Usage Examples:
# ============================================================================
#
# Build for development:
# docker build --target development -t py_template:dev .
#
# Build for runtime:
# docker build --target runtime -t py_template:latest .
#
# Build for testing:
# docker build --target testing -t py_template:test .
#
# Run development container:
# docker run -it --rm -v $(pwd):/app py_template:dev bash
#
# Run tests:
# docker run --rm py_template:test
#
# Run CLI:
# docker run --rm py_template:latest py_template info
#
# Interactive Python with py_template:
# docker run -it --rm py_template:latest python