-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
233 lines (159 loc) · 7.23 KB
/
Dockerfile
File metadata and controls
233 lines (159 loc) · 7.23 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# FROM scratch as ENV_SETUP
# can be ovveriden by --build-arg PY_VERSION=3.9.16
ARG PY_VERSION=3.12.9
FROM python:${PY_VERSION}-slim-bullseye AS python_slim
# ENV PY_RUNTIME=${PY_VERSION}
FROM python_slim AS builder
WORKDIR /app
COPY uv.lock pyproject.toml ./
# Install uv
COPY --from=ghcr.io/astral-sh/uv@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 /uv /uvx /bin/
# Export Exact/Pinned Prod (install only) dependencies, into pip format
FROM builder AS prod_builder
RUN uv export --no-dev --frozen --no-emit-project -f requirements-txt -o requirements.txt
# Export Exact/Pinned Prod + Test dependencies, into pip format
FROM builder AS test_builder
RUN uv export --no-dev --frozen --no-emit-project -f requirements-txt -o requirements-test.txt --extra test
# Export Exact/Pinned Prod + Docs dependencies, into pip format
FROM builder AS docs_builder
RUN uv export --no-dev --frozen --no-emit-project -f requirements-txt -o requirements.txt --extra docs
# Export Exact/Pinned Prod + Docs + Live Dev Server dependencies, into pip format
FROM builder AS docs_live_builder
RUN uv export --no-dev --frozen --no-emit-project -f requirements-txt -o requirements.txt --extra docslive
FROM scratch AS source
WORKDIR /app
COPY --from=prod_builder /app/requirements.txt .
# Copy Source Code
# COPY . .
COPY src src
COPY pyproject.toml .
COPY uv.lock .
COPY LICENSE .
COPY README.md .
FROM python_slim AS base_env
# Wheels Directory for Distro and its Dependencies (aka requirements)
ENV DISTRO_WHEELS=/app/dist
###### BUILD WHEELS STAGE ######
FROM base_env AS build_wheels
# Essential build tools
RUN apt-get update && \
apt-get install -y --no-install-recommends build-essential && \
pip install --no-cache-dir -U pip && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Essential build-time dependencies
COPY --from=ghcr.io/astral-sh/uv@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 /uv /uvx /bin/
# RUN pip install --no-cache-dir --upgrade pip && \
# pip install --no-cache-dir poetry-core && \
# pip install --no-cache-dir build
WORKDIR /app
COPY --from=source /app .
# Build Wheels for Distro's Dependencies, from /app/requirements.txt file
# Build Wheels for Distro's Package
RUN pip wheel --wheel-dir "${DISTRO_WHEELS}" -r ./requirements.txt && \
uv build --wheel --out-dir "/tmp/build-wheels" && \
mv /tmp/build-wheels/*.whl "${DISTRO_WHEELS}"
# Now all wheels are in DISTRO_WHEELS folder
FROM base_env AS install
# At runtime our app needs git binary
RUN apt-get update && \
apt-get install -y --no-install-recommends git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
# we copy the wheels built in 'build_wheels' stage
COPY --from=build_wheels ${DISTRO_WHEELS} dist
# Install wheels for our Distro and its Install/Runtime Dependencies
# in user site-packages (ie /root/.local/lib/python3.11/site-packages)
RUN pip install --no-deps --no-cache-dir --user ./dist/*.whl
##### TEST #####
## EDIT MODE TEST
FROM python_slim AS test_dev
WORKDIR /app
COPY --from=test_builder /app/requirements-test.txt .
# Install uv for faster test dependencies installation than 'pip install'
COPY --from=ghcr.io/astral-sh/uv@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 /uv /uvx /bin/
# Install test dependencies
RUN uv venv && \
uv pip install --no-cache-dir --no-deps -r requirements-test.txt
# Copy Source Code
COPY src src
COPY pyproject.toml .
# COPY poetry.lock .
COPY LICENSE .
COPY README.md .
# COPY tests tests
# Install in Editable Mode
RUN uv pip install --no-deps --no-cache-dir -e .
# Add Pytest, installed in uv controlled venv to PATH
ENV PATH="/app/.venv/bin:$PATH"
CMD [ "pytest", "-ra", "tests" ]
# CMD [ "uv", "run", "--locked", "--no-sync", "pytest", "-ra", "tests" ]
# docker build --target test_dev -t ela-test-dev .
# docker run --rm -v /data/repos/cookiecutter-python-package/.github/biskotaki.yaml:/app/.github/biskotaki.yaml -v /data/repos/cookiecutter-python-package/tests:/app/tests -it ela-test-dev
## TEST Prod Wheels
FROM base_env AS test_wheels
WORKDIR /app
# Install uv for faster test dependencies installation than 'pip install'
COPY --from=ghcr.io/astral-sh/uv@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 /uv /uvx /bin/
RUN uv venv
# Install Wheel of Package and Wheels its Prod dependencies
COPY --from=build_wheels ${DISTRO_WHEELS} dist
RUN uv pip install --no-deps --no-cache-dir ./dist/*.whl
# RUN pip install --no-deps --no-cache-dir "./dist/"
# Install test dependencies (pytest, pytest-object-getter, etc) from pypi
COPY --from=test_builder /app/requirements-test.txt .
RUN uv pip install --no-deps -r requirements-test.txt
# Add Pytest, installed in uv controlled venv to PATH
ENV PATH="/app/.venv/bin:$PATH"
# Configure Pytest: runner, test discovery, etc
COPY pyproject.toml .
CMD [ "pytest", "-ra", "tests" ]
# docker build --target test_wheels -t ela-test-wheels-dev .
# docker run --rm -v /data/repos/cookiecutter-python-package/.github/biskotaki.yaml:/app/.github/biskotaki.yaml -v /data/repos/cookiecutter-python-package/tests:/app/tests -it ela-test-wheels-dev
###### DOCS BASE ######
FROM python_slim AS docs_base
WORKDIR /app
# Install libenchant using package manage either apt or apk
RUN apt-get update && \
apt-get install -y --no-install-recommends enchant-2 git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
COPY src src
COPY pyproject.toml .
COPY poetry.lock .
COPY README.md .
# Add user's bin folder to PATH
ENV PATH="/root/.local/bin:$PATH"
###### DOCS - Build ######
FROM docs_base AS docs
# COPY --from=docs_builder requirements.txt .
# Install Prod + Docs dependencies
# RUN pip install --no-cache-dir --user -r requirements.txt
# Install in Editable Mode, since we don't care about wheels
RUN pip install --no-cache-dir --user -e .[docs] && \
pip install --no-cache-dir gitpython
# Copy Entrypoint inside the image (required since stage is not last in Dockerfile)
COPY scripts/sphinx-process.sh /app/scripts/sphinx-process.sh
# Building: docker build --target docs -t pygen-docs .
# Using:
# 1. For Building while enabling all Docs Checks:
# no build fail, no broken urls, no spelling mistakes!
# docker run --rm -v ${PWD}/docs:/app/docs -v ${PWD}/docs-dist:/app/docs-dist -it --entrypoint "/app/scripts/sphinx-process.sh" pygen-docs
## DOCS with Live Dev Server - Build ##
FROM docs_base AS docs_live
WORKDIR /app
# COPY --from=docs_live_builder requirements.txt .
# RUN pip install --no-cache-dir --user -r requirements.txt
# Install in Editable Mode, since we don't care about wheels
RUN pip install --no-cache-dir --user -e .[docslive]
# Building: docker build --target docs_live -t docs_live .
# Usage: For Serving live documentation (ie on localhost) with hot-reload
# docker run --rm -v ${PWD}/docs:/app/docs -v ${PWD}/docs-build:/app/docs-build -p 8000:8000 -it docs_live sphinx-autobuild --port 8000 --host 0.0.0.0 docs docs-build/html
## PROD ##
FROM install AS prod
# Add Pytest, installed in user's bin folder, to PATH
ENV PATH="/root/.local/bin:$PATH"
ENTRYPOINT [ "generate-python" ]
# docker build -t generate-python .
# docker run -it --rm -v ${PWD}/.github/biskotaki.yaml:/app/config.yml -v /tmp/gen:/tmp/gen generate-python --config-file /app/config.yml -o /tmp/gen --no-input