Skip to content

Commit b7685ad

Browse files
authored
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 <[email protected]>
1 parent 263ba37 commit b7685ad

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

fastapi/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim
2+
3+
WORKDIR /app
4+
5+
RUN apt update
6+
7+
COPY requirements.txt ./
8+
RUN pip install --no-cache-dir -r requirements.txt
9+
10+
COPY ./app ./app
11+

fastapi/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Compose sample application
2+
### Python/FastAPI application
3+
4+
Project structure:
5+
```
6+
├── docker-compose.yaml
7+
├── Dockerfile
8+
├── requirements.txt
9+
├── app
10+
   ├── main.py
11+
   ├── __init__.py
12+
13+
```
14+
15+
[_docker-compose.yaml_](docker-compose.yaml)
16+
```
17+
services:
18+
api:
19+
build: .
20+
container_name: fastapi-application
21+
environment:
22+
PORT: 8000
23+
ports:
24+
- '8000:8000'
25+
restart: "no"
26+
27+
```
28+
29+
## Deploy with docker-compose
30+
31+
```shell
32+
docker-compose up -d --build
33+
```
34+
## Expected result
35+
36+
Listing containers must show one container running and the port mapping as below:
37+
```
38+
$ docker ps
39+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40+
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
41+
```
42+
43+
After the application starts, navigate to `http://localhost:8000` in your web browser and you should see the following json response:
44+
```
45+
{
46+
"message": "OK"
47+
}
48+
```
49+
50+
Stop and remove the containers
51+
```
52+
$ docker-compose down
53+
```
54+
55+

fastapi/app/__init__.py

Whitespace-only changes.

fastapi/app/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastapi import FastAPI
2+
3+
app = FastAPI()
4+
5+
6+
@app.get("/")
7+
def hello_world():
8+
return {"message": "OK"}

fastapi/docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
api:
3+
build: .
4+
container_name: fastapi-application
5+
environment:
6+
PORT: 8000
7+
ports:
8+
- '8000:8000'
9+
restart: "no"
10+

fastapi/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fastapi
2+
uvicorn

0 commit comments

Comments
 (0)