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 <valon.januzaj98@gmail.com>
This commit is contained in:
Valon Januzaj 2021-10-10 23:38:34 +02:00 committed by GitHub
parent 263ba37b97
commit b7685ad15f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 0 deletions

11
fastapi/Dockerfile Normal file
View File

@ -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

55
fastapi/README.md Normal file
View File

@ -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
```

0
fastapi/app/__init__.py Normal file
View File

8
fastapi/app/main.py Normal file
View File

@ -0,0 +1,8 @@
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def hello_world():
return {"message": "OK"}

View File

@ -0,0 +1,10 @@
services:
api:
build: .
container_name: fastapi-application
environment:
PORT: 8000
ports:
- '8000:8000'
restart: "no"

2
fastapi/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
fastapi
uvicorn