32
loading...
This website collects cookies to deliver better user experience
Dockerfile
at the root of your working directory, and write the following instructions in it :FROM python:3.9
WORKDIR /app
# Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false
# Copy using poetry.lock* in case it doesn't exist yet
COPY ./app/pyproject.toml ./app/poetry.lock* /app/
RUN poetry install --no-root --no-dev
COPY ./app /app
CMD ["uvicorn", "app.main:app", "--reload", "--host", "0.0.0.0", "--port", "80"]
poetry
as a package manager, so we need to install it first andinstall
without creating a virtual env (that --no-root
bit). What's great about this approach is that you can usepyproject.toml
in your developement environment so things like static analysis tools uses the same dependencies. In a production poetry
to have a slimer image. app
folder will be created in the next section that's where our code will live. --reload
option we pass to uvicorn which will allows us to work without having to restart the worker everytime we makemkidr app
app
directory that will be our "main" polls
directory. mkdir app/app
to create the folder, and create a main.py
file inside of it. Your directory strucutre should now hello world !
is approaching. Inside main.py
write the following lines :from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
docker build -t poll-app .
docker run -it -p 80:80 -v "$(pwd)"/app:/app poll-app
. This will also mountlocalhost
and behold ! localhost/docs
you'll see the following : Hello world !
is out of the way (I recommend adding it to your personnal collection), let's get to the actual polls app. Aspolls
directory along side the main app. Once that's done add a file called endpoints.py
to __init__.py
file so the folder are recognized as Python modulespolls/endpoints.py
:from fastapi import APIRouter
router = APIRouter()
@router.get("/")
async def index():
return {"message": "polls index"}
FastaAPI()
. Now all we need to do to make our endpoint available, is to declare it in main.py
:app.include_router(polls.endpoints.router, prefix="/polls")
localhost/polls
and in the Swagger documentation ! Let's take a look !