From b7685ad15f6d64d44e6ec3d4c9ba7c2d012a8cdc Mon Sep 17 00:00:00 2001 From: Valon Januzaj Date: Sun, 10 Oct 2021 23:38:34 +0200 Subject: [PATCH] Add fastapi to the list of compose projects * Fastapi base Dockerfile * requirements.txt for fastapi project * Add documentation on how to run the application * Add entrypoint for fastapi application * Add docker-compose.yml for fastapi Signed-off-by: vjanz --- fastapi/Dockerfile | 11 ++++++++ fastapi/README.md | 55 ++++++++++++++++++++++++++++++++++++++ fastapi/app/__init__.py | 0 fastapi/app/main.py | 8 ++++++ fastapi/docker-compose.yml | 10 +++++++ fastapi/requirements.txt | 2 ++ 6 files changed, 86 insertions(+) create mode 100644 fastapi/Dockerfile create mode 100644 fastapi/README.md create mode 100644 fastapi/app/__init__.py create mode 100644 fastapi/app/main.py create mode 100644 fastapi/docker-compose.yml create mode 100644 fastapi/requirements.txt diff --git a/fastapi/Dockerfile b/fastapi/Dockerfile new file mode 100644 index 0000000..5b5bf8c --- /dev/null +++ b/fastapi/Dockerfile @@ -0,0 +1,11 @@ +FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim + +WORKDIR /app + +RUN apt update + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY ./app ./app + diff --git a/fastapi/README.md b/fastapi/README.md new file mode 100644 index 0000000..2dd0ff8 --- /dev/null +++ b/fastapi/README.md @@ -0,0 +1,55 @@ +## Compose sample application +### Python/FastAPI application + +Project structure: +``` +├── docker-compose.yaml +├── Dockerfile +├── requirements.txt +├── app +    ├── main.py +    ├── __init__.py + +``` + +[_docker-compose.yaml_](docker-compose.yaml) +``` +services: + api: + build: . + container_name: fastapi-application + environment: + PORT: 8000 + ports: + - '8000:8000' + restart: "no" + +``` + +## Deploy with docker-compose + +```shell +docker-compose up -d --build +``` +## Expected result + +Listing containers must show one container running and the port mapping as below: +``` +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +7087a6e79610 5c1778a60cf8 "/start.sh" About a minute ago Up About a minute 80/tcp, 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp fastapi-application +``` + +After the application starts, navigate to `http://localhost:8000` in your web browser and you should see the following json response: +``` +{ +"message": "OK" +} +``` + +Stop and remove the containers +``` +$ docker-compose down +``` + + diff --git a/fastapi/app/__init__.py b/fastapi/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fastapi/app/main.py b/fastapi/app/main.py new file mode 100644 index 0000000..9f6ec44 --- /dev/null +++ b/fastapi/app/main.py @@ -0,0 +1,8 @@ +from fastapi import FastAPI + +app = FastAPI() + + +@app.get("/") +def hello_world(): + return {"message": "OK"} diff --git a/fastapi/docker-compose.yml b/fastapi/docker-compose.yml new file mode 100644 index 0000000..bcd0c6e --- /dev/null +++ b/fastapi/docker-compose.yml @@ -0,0 +1,10 @@ +services: + api: + build: . + container_name: fastapi-application + environment: + PORT: 8000 + ports: + - '8000:8000' + restart: "no" + diff --git a/fastapi/requirements.txt b/fastapi/requirements.txt new file mode 100644 index 0000000..97dc7cd --- /dev/null +++ b/fastapi/requirements.txt @@ -0,0 +1,2 @@ +fastapi +uvicorn