first command to setup python env
python -m venv env
activate python env
source env/Scripts/activate
install Fastapi and unicorn
pip install fastapi uvicorn
make main.p
y
file
touch main.py
main.py
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
now start unicorn server
uvicorn main:app --reload --port=8000 --host=0.0.0.0
open this url
open swagger
close server
ctrl + c
now create requirements.txt file
pip freeze > requirements.txt
touch .dockerignore
.dockerignore
file
env/
__pycache__/
*.env
*.env.*
env.*
make docker-compose.yaml
file
touch docker-compose.yaml
docker-compose.yaml
file
version: '3'
services:
web:
build: .
command: sh -c "uvicorn main:app --reload --port=8000 --host=0.0.0.0"
ports:
- 8000:8000
volumes:
- .:/app
create Dockerfile
file
#
FROM python:3.9
#
WORKDIR /app
COPY . /app
#
RUN pip install --no-cache-dir --upgrade -r requirements.txt
now run this command to build and start docker container
docker compose up --build
now open these urls again
open this url
open swagger
to restart your docker build
docker compose up
That's it