Skip to content

Commit b6a4daf

Browse files
committed
refactor(docker)!: Migrate from Poetry to uv package manager and modernize Dockerfile
1 parent 2f8974a commit b6a4daf

1 file changed

Lines changed: 42 additions & 15 deletions

File tree

Dockerfile

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
1-
FROM python:3.11.4-slim-bullseye AS prod
1+
# Start from a slim version of Python 3.13
2+
FROM python:3.13-slim AS base
23

4+
# Set Python Envs
5+
ENV APP_HOME=/app/ PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 UV_COMPILE_BYTECODE=1
36

4-
RUN pip install poetry==1.8.2
7+
WORKDIR $APP_HOME
58

6-
# Configuring poetry
7-
RUN poetry config virtualenvs.create false
8-
RUN poetry config cache-dir /tmp/poetry_cache
9+
# Install in a single layer, and clean up in the same layer to minimize image size
10+
RUN apt-get update \
11+
&& apt-get install -y --no-install-recommends \
12+
libpq-dev \
13+
curl \
14+
ca-certificates \
15+
wget \
16+
&& pip install --prefer-binary --no-cache-dir --upgrade pip \
17+
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
18+
&& rm -rf /var/lib/apt/lists/* \
919

10-
# Copying requirements of a project
11-
COPY pyproject.toml poetry.lock /app/src/
12-
WORKDIR /app/src
20+
# Install uv
21+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
22+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
1323

14-
# Installing requirements
15-
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main
24+
# Place executables in the environment at the front of the path
25+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment
26+
ENV PATH="/app/.venv/bin:$PATH" UV_LINK_MODE=copy PYTHONPATH=$APP_HOME
1627

17-
# Copying actuall application
18-
COPY . /app/src/
19-
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main
28+
# ===== Prod stage =====
29+
FROM base AS prod
2030

21-
CMD ["/usr/local/bin/python", "-m", "FastAPI_super_template"]
31+
# Install base dependencies
32+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
33+
RUN --mount=type=cache,target=/root/.cache/uv \
34+
--mount=type=bind,source=uv.lock,target=uv.lock \
35+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
36+
uv sync --locked --no-install-project --no-dev
2237

38+
COPY ./pyproject.toml ./uv.lock $APP_HOME
39+
40+
COPY ./src $APP_HOME/src
41+
42+
# Sync the project
43+
RUN --mount=type=cache,target=/root/.cache/uv \
44+
uv sync --locked --no-dev
45+
46+
# Run app - exec form (doesn’t start a shell on its own)
47+
CMD ["/usr/local/bin/python", "-m", "main.py"]
48+
49+
# ===== Dev stage =====
2350
FROM prod AS dev
2451

25-
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install
52+
RUN uv sync --locked

0 commit comments

Comments
 (0)