Skip to content

Commit 864918e

Browse files
committed
Switch to uv in Dockerfile
1 parent 42f0d91 commit 864918e

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![Tests](https://github.com/jonasrenault/fastapi-react-mongodb-docker/actions/workflows/test.yml/badge.svg)
44
![Build](https://github.com/jonasrenault/fastapi-react-mongodb-docker/actions/workflows/build.yml/badge.svg)
55
[![License](https://img.shields.io/badge/License-MIT-yellow)](LICENSE)
6-
![python_version](https://img.shields.io/badge/Python-%3E=3.10-blue)
6+
![python_version](https://img.shields.io/badge/Python-%3E=3.12-blue)
77

88
This is a template application for a FARM stack. FARM stands for FastAPI, React, MongoDB.
99

backend/Dockerfile

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
FROM python:3.12-slim
1+
FROM python:3.12-slim-bookworm
22

33
ENV PYTHONUNBUFFERED=true
44

55
WORKDIR /app
66

7-
### install poetry
8-
RUN pip install poetry && poetry config virtualenvs.in-project true
7+
# Install uv
8+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
9+
COPY --from=ghcr.io/astral-sh/uv:0.5.11 /uv /uvx /bin/
910

10-
### install dependencies and project
11+
# Place executables in the environment at the front of the path
12+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment
13+
ENV PATH="/app/.venv/bin:$PATH"
14+
15+
# Compile bytecode
16+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode
17+
ENV UV_COMPILE_BYTECODE=1
18+
19+
# uv Cache
20+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching
21+
ENV UV_LINK_MODE=copy
22+
23+
# Install dependencies
24+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
25+
RUN --mount=type=cache,target=/root/.cache/uv \
26+
--mount=type=bind,source=uv.lock,target=uv.lock \
27+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
28+
uv sync --frozen --no-install-project
29+
30+
# Copy the project into the image
1131
ADD pyproject.toml README.md ./
1232
ADD app /app/app
13-
RUN poetry install --no-interaction --no-ansi
1433

15-
### add executables to path
16-
ENV PATH="/app/.venv/bin:$PATH"
34+
# Sync the project
35+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
36+
RUN --mount=type=cache,target=/root/.cache/uv \
37+
uv sync
1738

18-
### default cmd: run fastapi with 4 workers
39+
# default cmd: run fastapi with 4 workers
1940
CMD ["fastapi", "run", "--workers", "4", "app/main.py"]

backend/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@ This directory contains the backend API app. It is built with [FastAPI](https://
44

55
## Requirements
66

7-
* [Poetry](https://python-poetry.org/) for Python package and environment management.
7+
* [uv](https://docs.astral.sh/uv/) for Python package and environment management.
88
* A [MongoDB](https://www.mongodb.com/) database for persistence.
99

1010
## Install
1111

12-
The project uses poetry to manage dependencies and run the backend application. You can use an other tool to manage your virtual environment, such as `pip` or [uv](https://docs.astral.sh/uv/), but you'll need to extract the dependencies from the [pyproject.toml](./pyproject.toml) file.
13-
14-
If using poetry, for convenience, run `poetry config virtualenvs.in-project true` before installing depencies. This will install the dependencies in the project directory and make it easier to manage in vscode.
15-
16-
You can then install the dependencies with `poetry install --with dev`.
12+
The project uses [uv](https://docs.astral.sh/uv/) to manage dependencies and run the backend application. You can install the project and its dependencies with `uv sync`.
1713

1814
## Running the server
1915

2016
To start a development server, run
2117

2218
```console
23-
poetry run fastapi dev app/main.py
19+
uv run fastapi dev app/main.py
2420
```
2521

26-
from the `backend` directory (remove the `poetry run` prefix if using another dependency management tool).
22+
from the `backend` directory (remove the `uv run` prefix if using another dependency management tool).
2723

2824
This will start a server running on `http://127.0.0.1:8000`. The API will be available on the API's base prefix, which by default is `/api/v1`.
2925

@@ -36,7 +32,7 @@ Navigate to `http://localhost:8000/redoc` to access the API's alternative doc bu
3632
Run
3733

3834
```console
39-
poetry run pytest
35+
uv run pytest
4036
```
4137

4238
to run the unit tests for the backend app.

0 commit comments

Comments
 (0)