Skip to content

Commit bd72177

Browse files
committed
feat(docker): add uv support
1 parent 689f0b4 commit bd72177

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed
Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
1-
FROM python:{{ cookiecutter.python_version }}-slim-stretch
1+
{% if cookiecutter.dependency_management_tool == "uv" -%}
2+
FROM ghcr.io/astral-sh/uv:python{{ cookiecutter.python_version }}-bookworm-slim AS builder
3+
{%- else -%}
4+
FROM python:{{ cookiecutter.python_version }}-slim-bookworm AS builder
5+
{%- endif %}
26

3-
ARG USERNAME={{ cookiecutter.github_username.lower().replace(' ', '_').replace('-', '_') }}
7+
WORKDIR /app
8+
COPY . /app
9+
10+
{% if cookiecutter.dependency_management_tool == "uv" -%}
11+
ENV UV_COMPILE_BYTECODE=1 \
12+
UV_LINK_MODE=copy
13+
14+
RUN --mount=type=cache,target=/root/.cache/uv \
15+
--mount=type=bind,source=uv.lock,target=uv.lock \
16+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
17+
uv sync --frozen --no-install-project --no-dev
418

19+
RUN --mount=type=cache,target=/root/.cache/uv \
20+
uv sync --frozen --no-dev
21+
{%- else -%}
522
ENV PYTHONUNBUFFERED=1 \
623
PYTHONFAULTHANDLER=1 \
7-
PYTHONDONTWRITEBYTECODE=1 \
8-
PATH=$PATH:/home/$USERNAME/.local/bin
24+
PYTHONDONTWRITEBYTECODE=1
925

10-
RUN useradd --create-home $USERNAME
11-
USER $USERNAME
12-
WORKDIR /home/$USERNAME
26+
RUN python3 -m venv .venv
1327

14-
COPY . /home/$USERNAME/
28+
ENV PATH="/app/.venv/bin:$PATH"
1529

16-
RUN python3 -m pip install--no-cache-dir --upgrade pip && \
30+
RUN python3 -m pip install --no-cache-dir --upgrade pip && \
1731
python3 -m pip install --no-cache-dir -r requirements.txt
32+
{%- endif %}
33+
34+
35+
FROM python:{{ cookiecutter.python_version }}-slim-bookworm
36+
37+
ARG USERNAME={{ cookiecutter.github_username.lower().replace(' ', '_').replace('-', '_') }}
38+
39+
RUN useradd -m $USERNAME
40+
USER $USERNAME
41+
WORKDIR /app
42+
43+
COPY --from=builder --chown=$USERNAME:$USERNAME /app /app
44+
45+
ENV PATH="/app/.venv/bin:$PATH"

{{cookiecutter.project_slug}}/tasks/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def dist(ctx: Context) -> None:
3232
def docker(ctx: Context) -> None:
3333
"""Build docker image"""
3434
{% if cookiecutter.dependency_management_tool == "uv" -%}
35-
ctx.run("uv export --format requirements-txt > requirements.txt")
35+
ctx.run("uv lock")
3636
{%- elif cookiecutter.dependency_management_tool == "poetry" -%}
3737
ctx.run("poetry export -f requirements.txt -o requirements.txt")
3838
{%- elif cookiecutter.dependency_management_tool == "pipenv" -%}
39-
ctx.run("pipenv lock --keep-outdated --requirements > requirements.txt")
39+
ctx.run("pipenv lock && pipenv requirements")
4040
{%- endif %}
4141
user_name = "{{ cookiecutter.github_username.lower().replace(' ', '_').replace('-', '_') }}"
4242
proj_name = "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}"

0 commit comments

Comments
 (0)