This project demonstrates a Flask application with MongoDB integration using Docker Compose.
The repo is broken up into version numbers so you can follow along at different points in the development cycle.
- python:3.11-slim
- mongo:7.0.11
- Flask==3.0.3
- pymongo==4.7.3
- Mac Studio
- Apple M1 Max
- macOS Sonoma 14.5
- IntelliJ IDEA 2024.1.3
- Docker v4.19.0
Install Docker and Docker Compose
https://www.docker.com/products/docker-desktop/
Download and install MongoDB Compass
https://www.mongodb.com/products/tools/compass
-
Create a directory named
Flask-Mongodb-Demo:mkdir Flask-Mongodb-Demo
-
Change the current directory to
Flask-Mongodb-Demo:cd Flask-Mongodb-Demo -
Clone the repository from GitHub into the current directory:
git clone https://github.com/UMiamiLibraries/flask-mongodb-demo.git
-
Change the current directory to the cloned repository directory:
cd flask-mongodb-demo -
Verify the current status of the Git repository:
git status
-
Fetch all tags
git fetch --all --tags
-
(Optional) List all tags
git tag
-
Checkout a specific tag and create a new branch
git checkout tags/v0.2 -b v0.2-branch
- Run
docker-compose up -dto start the application - Access the application at
http://localhost:5000
- Connect to
mongodb://localhost:27017 - You can now view and manipulate the database using the GUI
-
In MongoDB Compass, connect to
mongodb://localhost:27017 -
You should see a database called
library_db. If not, create it. -
Inside
library_db, create a new collection calledbooks. -
To add a sample document, click on the
bookscollection, then click "Add Data" > "Insert Document". -
Enter the following JSON and click "Insert":
{ "title": "To Kill a Mockingbird", "author": "Harper Lee", "isbn": "9780446310789", "published_year": 1960, "genre": "Fiction" } -
You should now see this document in your
bookscollection.
To make changes to the application:
- Modify the files in the
appdirectory - The changes will be reflected immediately due to volume mounting
To add new dependencies:
- Add them to
docker/requirements.txt - Rebuild the Docker image:
docker-compose build - Restart the containers:
docker-compose up -d