iTranslated by AI
[2024 Edition] Effortlessly Building a Python API Server (Docker + FastAPI + MongoDB)
What do I want to do?
This is about easily setting up a Python-based API server using Docker and FastAPI.
Note that this article is intended as a personal memo for myself.
I won't provide specific explanations for the commands. Just Google it.
This time, I've pieced together information found online (official references, etc.) without thinking too much. There might be some redundant commands, so please adjust them as needed.
By the way, I'm using MongoDB as the database, which is influenced by a hand-over of a system that used ElasticSearch. So, feel free to use whatever database you prefer.
Prerequisites
This guide assumes the following are already installed.
The environment used is WSL2 on Windows 11, but the commands should work similarly on Linux.
- python3
- docker
Configuration
0. File Hierarchy
The default file hierarchy is as follows.
Please use it as a reference. Items with a trailing / are folders.
- /
- api-src/
- main.py
- requirements.txt
- mongo/
- docker-compose.yml
- Dockerfile-api
- .env
- api-src/
1. Docker File Configuration
services:
mongo:
image: mongo
networks:
- mongo-compose-network
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- ./mongo:/data/db
user: ${UID}:${GID}
mongo-express:
networks:
- mongo-compose-network
image: mongo-express
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: password
ME_CONFIG_MONGODB_URL: "mongodb://root:example@mongo:27017/"
api-sever:
networks:
- mongo-compose-network
build:
context: .
dockerfile: Dockerfile-api
ports:
- 8080:8000
volumes:
- ./api-src:/usr/src/app:ro
networks:
mongo-compose-network:
driver: bridge
FROM python:3
WORKDIR /usr/src/app
COPY ./api-src/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
UID=1002
GID=1002
For .env, please enter the UID and GID of the Linux account you are using.
2. Python Hello World and requirements.txt
from fastapi import FastAPI
app = FastAPI()
@app.get("/hello")
async def hello():
return {"message": "hello world! (/Hello)"}
@app.get("/")
async def hello():
return {"message": "hello world (Home)!"}
Generate requirements.txt by running the following command.
Note that you should regenerate requirements.txt every time you add a library.
python3 -m pip freeze > requirements.txt
Startup
When Updating Dockerfile or requirements.txt
docker compose up --build
For General Startup or Source Code Changes
docker compose up
Note: If you change the source code, it will automatically reload without having to restart the container.
The API server can be accessed at port 8080 (http://localhost:8080).
Also, if you want to check the database contents, you can do so at port 8081 (http://localhost:8081).
The End!
Thank you for reading this far! Great job!!!
Discussion