From 4ebe9eeec3c34986b6e640519d9525c7e2563ee2 Mon Sep 17 00:00:00 2001 From: Rafael Bradley Date: Thu, 16 Jul 2026 00:39:01 -0300 Subject: [PATCH] Add default Dockerfile --- Dockerfile | 14 ++++++++++++++ README.md | 52 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..99661e0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM git.moan.dev/docker/python/with-uv:3.14-alpine + +# Only copy uv-related files, necessary for syncing and caching. +WORKDIR /app +COPY pyproject.toml uv.lock /app/ + +# Run with --no-dev on not to install dev dependencies. +RUN python3 -m uv sync --no-dev + +# Copy the rest of your app. +COPY . /app/ + +ENTRYPOINT [ "/bin/sh", "-c" ] +CMD [ "cd /app && python3 -m uv run --no-dev uvicorn project:app --workers ${WORKERS:-$(nproc)} --host ${BIND_HOST:-0.0.0.0} --port ${BIND_PORT:-8000}" ] diff --git a/README.md b/README.md index f84604d..ecd142f 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ A template FastAPI project with Tortoise ORM integration. This project is divided into 3 sub-modules, `project.api`, `project.db`, and `project.lib`. -- `project.api`: All the API-related code and the public-facing part of your code. -- `project.db`: DB models, fields, migration, setup, and more. The storage part of - your code. -- `project.lib`: All the business logic of your code. External API calls, internal - utilities, and any other internal code goes here. +- `project.api`: All the API-related code and the public-facing part of your code. +- `project.db`: DB models, fields, migration, setup, and more. The storage part of + your code. +- `project.lib`: All the business logic of your code. External API calls, internal + utilities, and any other internal code goes here. ### API @@ -227,8 +227,8 @@ When calling that endpoint, the error will look like: ```json { - "title": "Not Found", - "message": "The requested duck was not found" + "title": "Not Found", + "message": "The requested duck was not found" } ``` @@ -289,18 +289,18 @@ about ORM usage. The `project.db` module has a few sub-modules whose purpose can be inferred based on the naming. The modules you'll most commonly edit are the following: -- `project.db` (`__init__.py`): This file re-exports models to make importing - elsewhere easier. -- `project.db.models`: Contains concern-specific submodules with database model - definitions. For example, `users.py` for `User` models, `books.py` for `Book` and - `Author` models (e.g. in a books-related application). -- `project.db.migrations`: Contains migrations, autogenerated by the `tortoise` CLI. -- `project.db.fields`: Custom DB fields and field aliases. It contains a - `NanoIDField` function which aliases to a nanoid `CharField`. Add any custom DB - fields here. -- `project.db.lifespan`: Contains `on_startup()` and `on_shutdown()`. They get called - from `project.api.lifespan` when the API goes up and down, applying migrations and - initializing DB connections on startup and closing connections on shutdown. +- `project.db` (`__init__.py`): This file re-exports models to make importing + elsewhere easier. +- `project.db.models`: Contains concern-specific submodules with database model + definitions. For example, `users.py` for `User` models, `books.py` for `Book` and + `Author` models (e.g. in a books-related application). +- `project.db.migrations`: Contains migrations, autogenerated by the `tortoise` CLI. +- `project.db.fields`: Custom DB fields and field aliases. It contains a + `NanoIDField` function which aliases to a nanoid `CharField`. Add any custom DB + fields here. +- `project.db.lifespan`: Contains `on_startup()` and `on_shutdown()`. They get called + from `project.api.lifespan` when the API goes up and down, applying migrations and + initializing DB connections on startup and closing connections on shutdown. #### Model Modules @@ -445,7 +445,19 @@ your now-renamed source code folder. To start the server, run: ```sh -$ uv run uvicorn project:app --reload +$ uv run uvicorn project:app --reload --reload-include "docs/**/*.md" ``` `project` is your import name. + +This template ships with a `Dockerfile` that will work out of the box by default in +most cases. By default, it uses the following environment variables: + +| Name | Description | Possible Values | Default | +| ----------- | -------------------------------------------- | -------------------------------------- | ---------- | +| `BIND_HOST` | The IP address to bind the server to. | Available IP addresses. | `0.0.0.0` | +| `BIND_PORT` | The port to bind the server to. | Unused IP address ports. | `8000` | +| `WORKERS` | The amount of workers to start Uvicorn with. | An integer greater than or equal to 1. | `$(nproc)` | + +If you're prefixing your environment variables with your project's name, make sure to +edit the `Dockerfile`'s `CMD` statement to reflect those changes.