Postgres app exit4/11/2023 ![]() ![]() dockerfile : Dockerfile.prod expose : # new - 80 environment : - DATABASE_URL=postgresql://fastapi_traefik_prod: :5432/fastapi_traefik_prod depends_on : - db labels : # new - "traefik.enable=true" - ".rule=Host(``)" - ".tls=true" - ".tls.certresolver=letsencrypt" db : image : postgres:15-alpine volumes : - postgres_data_prod:/var/lib/postgresql/data/ expose : - 5432 environment : - POSTGRES_USER=fastapi_traefik_prod - POSTGRES_PASSWORD=fastapi_traefik_prod - POSTGRES_DB=fastapi_traefik_prod traefik : # new build : context. # version : '3.8' services : web : build : context. dashboard = true enables the monitoring dashboard.exposedByDefault = false unexposes all services..entryPoint redirects all insecure requests to the secure port.entryPoints.websecure sets the entry point for our secure HTTPS application to port 443.entryPoints.web sets the entry point for our insecure HTTP application to port 80.Make sure to replace with your actual email address. # address = ":80" to = "websecure" scheme = "https" address = ":443" dashboard = true exposedByDefault = false email = " " storage = "/certificates/acme.json" entryPoint = "web" To test, first bring down any existing containers: /var/run/docker.sock:/var/run/docker.sock:ro enables Traefik to discover other containers.:/etc/traefik/traefik.toml maps the local config file to the config file in the container so that the settings are kept in sync Take note of the volumes within the traefik service: .rule=Host(`fastapi.localhost`) when the request has Host=fastapi.localhost, the request is redirected to this service.traefik.enable=true enables Traefik to discover the service. ![]() We also added the following labels to the web service: :/app expose : # new - 8000 environment : - DATABASE_URL=postgresql://fastapi_traefik: :5432/fastapi_traefik depends_on : - db labels : # new - "traefik.enable=true" - ".rule=Host(`fastapi.localhost`)" db : image : postgres:15-alpine volumes : - postgres_data:/var/lib/postgresql/data/ expose : - 5432 environment : - POSTGRES_USER=fastapi_traefik - POSTGRES_PASSWORD=fastapi_traefik - POSTGRES_DB=fastapi_traefik traefik : # new image : traefik:v2.9.6 ports : - 8008:80 - 8081:8080 volumes : - "./:/etc/traefik/traefik.toml" - "/var/run/docker.sock:/var/run/docker.sock:ro" volumes : postgres_data :įirst, the web service is only exposed to other containers on port 8000. ![]() command : bash -c 'while !docker-compose.yml version : '3.8' services : web : build. You can check that the volume was created as well by running: You are now connected to database "fastapi_traefik" as user "fastapi_traefik". ( 4 rows ) fastapi_traefik = # \c fastapi_traefik Template1 | fastapi_traefik | UTF8 | en_US.utf8 | en_US.utf8 | =c/fastapi_traefik | | | | | fastapi_traefik =CTc/fastapi_traefik ![]() Template0 | fastapi_traefik | UTF8 | en_US.utf8 | en_US.utf8 | =c/fastapi_traefik Postgres | fastapi_traefik | UTF8 | en_US.utf8 | en_US.utf8 | Name | Owner | Encoding | Collate | Ctype | Access privileges $ docker-compose exec db psql -username =fastapi_traefik -dbname =fastapi_traefik Next, update app/main.py to connect to the database and add a dummy user: Since the tables are created using SQLAlchemy (under the hood), database migration is possible via Alembic.Ĭheck out Alembic usage, from the official ormar documentation, for more on using Alembic with ormar. Note that each ormar.Model is also a pydantic.BaseModel, so all pydantic methods are also available on a model. Ormar uses SQLAlchemy for creating databases/tables and constructing database queries, databases for executing the queries asynchronously, and pydantic for data validation. This will create a pydanic model and a SQLAlchemy table. Boolean ( default = True, nullable = False ) engine = sqlalchemy. String ( max_length = 128, unique = True, nullable = False ) active : bool = ormar. Integer ( primary_key = True ) email : str = ormar. Model ): class Meta ( BaseMeta ): tablename = "users" id : int = ormar. ModelMeta ): metadata = metadata database = database class User ( ormar. config import settings database = databases. # app/db.py import databases import ormar import sqlalchemy from. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |