Skip to content

Commit 86100d5

Browse files
authored
Merge pull request #40 from smkent/docker
Separate build and run stages in template `Dockerfile`
2 parents 967f9aa + ed9fd82 commit 86100d5

2 files changed

Lines changed: 56 additions & 28 deletions

File tree

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
{%- import "includes/python.jinja" as py with context -%}
2-
FROM ghcr.io/astral-sh/uv:python{{ py.latest() }}-bookworm-slim
2+
FROM ghcr.io/astral-sh/uv:python{{ py.latest() }}-bookworm-slim AS builder
33

44
WORKDIR /app
55

66
# Install dependencies first (cached layer, only invalidated by pyproject.toml changes)
7-
COPY pyproject.toml ./
7+
COPY pyproject.toml README.md .
88
RUN --mount=type=cache,target=/root/.cache/uv \
99
uv sync --no-dev --no-install-project
1010

1111
# Copy source and install project
1212
COPY {{ project_slug }}/ ./{{ project_slug }}/
1313
RUN --mount=type=cache,target=/root/.cache/uv \
14-
uv sync --no-dev
14+
uv sync --no-dev --no-editable
15+
16+
FROM python:{{ py.latest() }}-slim
17+
18+
COPY --from=builder /app/.venv /app/.venv
1519

1620
RUN useradd --no-create-home --shell /bin/false app
1721
USER app
1822

19-
CMD ["uv", "run", "python", "-m", "{{ project_slug }}"]
23+
CMD ["/app/.venv/bin/python", "-m", "{{ project_slug }}"]
2024
{#- vim: set ft=dockerfile: #}
Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,163 @@
11
# serializer version: 1
22
# name: test_workflows_container[3.12-3.10]
33
'''
4-
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
4+
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
55

66
WORKDIR /app
77

88
# Install dependencies first (cached layer, only invalidated by pyproject.toml changes)
9-
COPY pyproject.toml ./
9+
COPY pyproject.toml README.md .
1010
RUN --mount=type=cache,target=/root/.cache/uv \
1111
uv sync --no-dev --no-install-project
1212

1313
# Copy source and install project
1414
COPY pkfire/ ./pkfire/
1515
RUN --mount=type=cache,target=/root/.cache/uv \
16-
uv sync --no-dev
16+
uv sync --no-dev --no-editable
17+
18+
FROM python:3.12-slim
19+
20+
COPY --from=builder /app/.venv /app/.venv
1721

1822
RUN useradd --no-create-home --shell /bin/false app
1923
USER app
2024

21-
CMD ["uv", "run", "python", "-m", "pkfire"]
25+
CMD ["/app/.venv/bin/python", "-m", "pkfire"]
2226

2327
'''
2428
# ---
2529
# name: test_workflows_container[3.12-3.12]
2630
'''
27-
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
31+
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
2832

2933
WORKDIR /app
3034

3135
# Install dependencies first (cached layer, only invalidated by pyproject.toml changes)
32-
COPY pyproject.toml ./
36+
COPY pyproject.toml README.md .
3337
RUN --mount=type=cache,target=/root/.cache/uv \
3438
uv sync --no-dev --no-install-project
3539

3640
# Copy source and install project
3741
COPY pkfire/ ./pkfire/
3842
RUN --mount=type=cache,target=/root/.cache/uv \
39-
uv sync --no-dev
43+
uv sync --no-dev --no-editable
44+
45+
FROM python:3.12-slim
46+
47+
COPY --from=builder /app/.venv /app/.venv
4048

4149
RUN useradd --no-create-home --shell /bin/false app
4250
USER app
4351

44-
CMD ["uv", "run", "python", "-m", "pkfire"]
52+
CMD ["/app/.venv/bin/python", "-m", "pkfire"]
4553

4654
'''
4755
# ---
4856
# name: test_workflows_container[3.14-3.10]
4957
'''
50-
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim
58+
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim AS builder
5159

5260
WORKDIR /app
5361

5462
# Install dependencies first (cached layer, only invalidated by pyproject.toml changes)
55-
COPY pyproject.toml ./
63+
COPY pyproject.toml README.md .
5664
RUN --mount=type=cache,target=/root/.cache/uv \
5765
uv sync --no-dev --no-install-project
5866

5967
# Copy source and install project
6068
COPY pkfire/ ./pkfire/
6169
RUN --mount=type=cache,target=/root/.cache/uv \
62-
uv sync --no-dev
70+
uv sync --no-dev --no-editable
71+
72+
FROM python:3.14-slim
73+
74+
COPY --from=builder /app/.venv /app/.venv
6375

6476
RUN useradd --no-create-home --shell /bin/false app
6577
USER app
6678

67-
CMD ["uv", "run", "python", "-m", "pkfire"]
79+
CMD ["/app/.venv/bin/python", "-m", "pkfire"]
6880

6981
'''
7082
# ---
7183
# name: test_workflows_container[3.14-3.12]
7284
'''
73-
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim
85+
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim AS builder
7486

7587
WORKDIR /app
7688

7789
# Install dependencies first (cached layer, only invalidated by pyproject.toml changes)
78-
COPY pyproject.toml ./
90+
COPY pyproject.toml README.md .
7991
RUN --mount=type=cache,target=/root/.cache/uv \
8092
uv sync --no-dev --no-install-project
8193

8294
# Copy source and install project
8395
COPY pkfire/ ./pkfire/
8496
RUN --mount=type=cache,target=/root/.cache/uv \
85-
uv sync --no-dev
97+
uv sync --no-dev --no-editable
98+
99+
FROM python:3.14-slim
100+
101+
COPY --from=builder /app/.venv /app/.venv
86102

87103
RUN useradd --no-create-home --shell /bin/false app
88104
USER app
89105

90-
CMD ["uv", "run", "python", "-m", "pkfire"]
106+
CMD ["/app/.venv/bin/python", "-m", "pkfire"]
91107

92108
'''
93109
# ---
94110
# name: test_workflows_container[No maximum-3.10]
95111
'''
96-
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim
112+
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim AS builder
97113

98114
WORKDIR /app
99115

100116
# Install dependencies first (cached layer, only invalidated by pyproject.toml changes)
101-
COPY pyproject.toml ./
117+
COPY pyproject.toml README.md .
102118
RUN --mount=type=cache,target=/root/.cache/uv \
103119
uv sync --no-dev --no-install-project
104120

105121
# Copy source and install project
106122
COPY pkfire/ ./pkfire/
107123
RUN --mount=type=cache,target=/root/.cache/uv \
108-
uv sync --no-dev
124+
uv sync --no-dev --no-editable
125+
126+
FROM python:3.14-slim
127+
128+
COPY --from=builder /app/.venv /app/.venv
109129

110130
RUN useradd --no-create-home --shell /bin/false app
111131
USER app
112132

113-
CMD ["uv", "run", "python", "-m", "pkfire"]
133+
CMD ["/app/.venv/bin/python", "-m", "pkfire"]
114134

115135
'''
116136
# ---
117137
# name: test_workflows_container[No maximum-3.12]
118138
'''
119-
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim
139+
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim AS builder
120140

121141
WORKDIR /app
122142

123143
# Install dependencies first (cached layer, only invalidated by pyproject.toml changes)
124-
COPY pyproject.toml ./
144+
COPY pyproject.toml README.md .
125145
RUN --mount=type=cache,target=/root/.cache/uv \
126146
uv sync --no-dev --no-install-project
127147

128148
# Copy source and install project
129149
COPY pkfire/ ./pkfire/
130150
RUN --mount=type=cache,target=/root/.cache/uv \
131-
uv sync --no-dev
151+
uv sync --no-dev --no-editable
152+
153+
FROM python:3.14-slim
154+
155+
COPY --from=builder /app/.venv /app/.venv
132156

133157
RUN useradd --no-create-home --shell /bin/false app
134158
USER app
135159

136-
CMD ["uv", "run", "python", "-m", "pkfire"]
160+
CMD ["/app/.venv/bin/python", "-m", "pkfire"]
137161

138162
'''
139163
# ---

0 commit comments

Comments
 (0)