A lightweight and efficient backend web server built using FastAPI and MongoDB, ideal for modern Python-based RESTful APIs.
- ⚡ FastAPI for high-performance async API routes
- 🍃 MongoDB Atlas integration for cloud-hosted NoSQL storage
- 🛠️ Clean structure and support for
.env
secrets - ♻ Auto-reload server on code changes during development
- 🐳 Dockerized for easy deployment and environment consistency
Install the required Python packages using:
pip install fastapi uvicorn pymongo python-dotenv
pip install fastapi uvicorn pymongo
- Go to https://www.mongodb.com/
- Log in
- Click homepage logo > Project (Create New) > Connect > Drivers
- Create a cluster and connect it via Drivers → Python
- python -m pip install "pymongo[srv]"
- Ensure the IP you are currently using is set as an allowed IP (within your MongoDB account within 'Network Access'.)
uvicorn main:app --reload --no-use-colors
This project is fully Dockerized for consistent development and deployment environments.
docker build -t fastapi-todo-app .
docker run -d -p 8000:8000 --env-file .env fastapi-todo-app
docker run -d -p 8000:8000 -v $(pwd):/app --env-file .env fastapi-todo-app uvicorn main:app --reload --host 0.0.0.0 --port 8000
docker run -d -p 8000:8000 -v %cd%:/app --env-file .env fastapi-todo-app uvicorn main:app --reload --host 0.0.0.0 --port 8000
Notes
- fastapi-todo-app
- name of the Docker image
- uvicorn main:app
- Start FastAPI app with Uvicorn
- --reload
- Enables live-reload on code changes
- --host 0.0.0.0
- Binds the app to all network interfaces inside the container, so it’s accessible from outside
- --port 8000
- Exposes the app on port 8000 inside the container
docker run -d -p 8000:8000 --env-file .env fastapi-todo-app uvicorn main:app --host 0.0.0.0 --port 8000
docker build -t fastapi .
docker images
docker run -p 8080:80 fastapi
- To view web server go to web browser and visit the URL: localhost:8080
docker compose up --build
docker ps
- View the system logs
- Get the container ID, from running docker ps
- 4a88d5012d41
docker attach 4a88d5012d41
E.g:
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [16]
INFO: Stopping reloader process [1]
docker compose up
- -it: Interactive terminal access
- /bin/bash: Open the terminal within the directory with the project
docker exec -it 4a88d5012d41 /bin/bash
- Check the contents of the directory to confirm that this is the correct directory
ls