{"id":15192076,"url":"https://github.com/sply88/fastapi-sqlmodel-docker-starter","last_synced_at":"2026-02-15T01:32:21.410Z","repository":{"id":181053338,"uuid":"666139924","full_name":"sply88/fastapi-sqlmodel-docker-starter","owner":"sply88","description":"Example project to illustrate usage of FastApi and SQLModel in conjunction with Docker Compose.","archived":false,"fork":false,"pushed_at":"2023-07-13T20:17:15.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-12T22:12:54.111Z","etag":null,"topics":["docker","dockercompose","fastapi","postgresql","python","sqlmodel"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sply88.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-07-13T20:05:31.000Z","updated_at":"2023-07-13T20:24:34.000Z","dependencies_parsed_at":"2023-07-13T21:36:49.555Z","dependency_job_id":null,"html_url":"https://github.com/sply88/fastapi-sqlmodel-docker-starter","commit_stats":null,"previous_names":["sply88/fastapi-sqlmodel-docker-starter"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sply88/fastapi-sqlmodel-docker-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sply88%2Ffastapi-sqlmodel-docker-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sply88%2Ffastapi-sqlmodel-docker-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sply88%2Ffastapi-sqlmodel-docker-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sply88%2Ffastapi-sqlmodel-docker-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sply88","download_url":"https://codeload.github.com/sply88/fastapi-sqlmodel-docker-starter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sply88%2Ffastapi-sqlmodel-docker-starter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29464076,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-15T01:01:38.065Z","status":"ssl_error","status_checked_at":"2026-02-15T01:01:23.809Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","dockercompose","fastapi","postgresql","python","sqlmodel"],"created_at":"2024-09-27T21:04:45.181Z","updated_at":"2026-02-15T01:32:21.394Z","avatar_url":"https://github.com/sply88.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI with SQLModel and Docker Compose\n\nExample project to illustrate usage of [FastAPI](https://fastapi.tiangolo.com/) \nand [SQLModel](https://sqlmodel.tiangolo.com/) in conjunction with \n[Docker Compose](https://docs.docker.com/compose/).\n\n## Overview\n\nThe example application is a simple REST API that allows clients to `GET` `POST` \n`PATCH` and `DELETE` \"messages\". These are composed of the actual message text \nand a label that is either \"unknown\", \"funny\" or \"informative\". The messages are \nstored in a [PostgreSQL](https://www.postgresql.org/) database.\n\nOn the one hand, I hope the examples are helpful and informative for people\nstudying the above frameworks and technologies.  \nOn the other hand, this project should provide a skeleton, that can be used as\na starter for (small) real-world projects. \n\nTopics addressed are\n- Example application using FastAPI and SQLModel\n- Testing the application\n- Assembling multiple services with docker compose\n- Configuring these services using environment variables\n- Suggested conventions for handling this configuration in the application code to\nfacilitate code readability and testability\n\n\n## Development\n\n**Prerequisites**:\nThe following section assumes that Docker and Docker Compose are installed.\nSee [here](https://docs.docker.com/engine/install/) for directions.\n\n### Quick Start\n\nInstall development requirements via\n```\npip install -r requirements.txt\n```\n\nand start the development stack via\n\n```\ndocker compose -f docker-compose.yml -f dev.yml --env-file dev.env up -d\n```\n\nNavigate to http://127.0.0.1:8000/docs to explore the interactive API documentation.\n\nRun tests with\n```\npytest ./api\n```\nand code style checks with\n```\nflake8 ./api\n```\n\n\nStop the development stack with\n```\ndocker compose -f docker-compose.yml -f dev.yml --env-file dev.env down\n```\n\n### Details\n\n#### Docker Compose\n\nThere is one directory per service defined in [docker-compose.yml](docker-compose.yml).\nThe directory [./api](./api) contains all files for the FastAPI application,\nespecially the source code. The directory [./postgres](./postgres) contains files \nonly relevant to the postgresql database, e.g. the script for user initialization.\n\nAs illustrated in the previous Quick Start section, any Docker Compose `COMMAND`\n(`up`, `build` etc.) is used like this during development:\n```\ndocker compose -f docker-compose.yml -f dev.yml --env-file dev.env COMMAND\n```\n\n[docker-compose.yml](docker-compose.yml) contains the base configuration of the\nservices that is complemented with the configuration in [dev.yml](./dev.yml).\nThe latter configuration file adjust the basic configuration for development\nconvenience by:\n- Mounting the API source code and running uvicorn with the `--reload` flag.\nThis way, the effects of code changes are applied immediately and there is no \nneed to rebuild the image to test each change during development.\n- The postgres service is exposed to the host, so one can easily connect\nto the development database with tools such as [pgadmin](https://www.pgadmin.org/)\nfor debugging, etc.\n- An additional database service `postgres-test` is included that will be used\nduring tests (note the pytest fixture `db_uri` in \n[./api/tests/conftest.py](./api/tests/conftest.py)). The rationale for using \nan extra DB is, that tests expect an empty DB, that might be seeded with specific\ndata for a given test case.\n\nThe configuration of the development environment is completed by [dev.env](dev.env),\nwhich is the only env file kept in version control and should never be used in a\nproduction setting.  \nThe Docker Compose files (docker-compose.yml, dev.yml) show how the values set \nin the `--env-file` are passed to the service containers as environment variables.\n\nFor more information on combining multiple compose files and setting environment \nvariables refer to the official docker documentation \n[here](https://docs.docker.com/compose/extends/#multiple-compose-files) and \n[here](https://docs.docker.com/compose/environment-variables/),\nrespectivey.\n\n#### The FastAPI application\n\nThe application source code is located in [api/api](./api/api). The subpackage\n`data_management` contains data model definitions as well as CRUD and database\nconnection utils. The subpackage `routers` contains the actual definition of\nthe applications HTTP interface.  \nThe model naming scheme in \n[api/api/data_management/models.py](api/api/data_management/models.py) and usage \nof \n[fastapi's dependency injection mechanism](https://fastapi.tiangolo.com/tutorial/dependencies/)\nfor database sessions in [api/api/routers/messages.py](api/api/routers/messages.py) \nwas inspired by \n[this excellent sqlmodel tutorial](https://sqlmodel.tiangolo.com/tutorial/fastapi/).\n\nThe main entrypoint of the application is [api.main](./api/api/main.py). It \nassembles and sets up the different lower level components.\n\nFinally, [`api.settings.Settings`](./api/api/settings.py) is responsible for\nreading any environment configuration using \n[Pydantic Settings Management](https://docs.pydantic.dev/latest/usage/pydantic_settings/).\nNote that the attribute names of the `Settings` class are just lower case versions\nof the variable names passed to the API container in [docker-compose.yml](docker-compose.yml),\nand hence pydantic will load those values automatically from the environment variables.\n\nThe application uses the following convention for configuration handling:\n`api.settings.Settings` is the only place that reads variables from the environment.\nThe main entrypoint `api.main` resolves this configuration and passes the required\ndependencies to lower level components, e.g. `from .settings import Settings` happens \nonly in `api.main` and the `db_uri` is passed down to the class responsible for\nestablishing database connections.\n\nSeparating configuration from the application code in this way, facilitates\ntestability, and specifically the usage of tools such as \n[pytest fixtures](https://docs.pytest.org/en/6.2.x/fixture.html)\nand [fastapi dependency_overrides](https://fastapi.tiangolo.com/advanced/testing-dependencies/).\nIt should also improve readability, as developers can easily comprehend how\ncertain environment settings affect the application without searching the code\nbase for config classes or `os.getenv` calls.\n\n## Deployment\n\n**WIP**\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsply88%2Ffastapi-sqlmodel-docker-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsply88%2Ffastapi-sqlmodel-docker-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsply88%2Ffastapi-sqlmodel-docker-starter/lists"}