Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sweep: Feature Request: Implement MongoDB Atlas Locally Using Docker [Backlog - Least Priority] (βœ“ Sandbox Passed) #17

Closed
8 changes: 7 additions & 1 deletion API/database.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import os
from datetime import datetime

from pymongo import MongoClient


class Database:
def __init__(self, uri="mongodb://localhost:27017/", db_name="ImageDB"):
def __init__(self, db_name="ImageDB"):
use_docker_db = os.environ.get('USE_DOCKER_DB', 'False') == 'True'
if use_docker_db:
uri = "mongodb://admin:password@localhost:27017/"
else:
uri = "mongodb://localhost:27017/"
self.client = MongoClient(uri)
self.db = self.client[db_name]

Expand Down
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Use the official MongoDB image as the base
FROM mongo:latest

# Set environment variables for MongoDB
ENV MONGO_INITDB_ROOT_USERNAME=admin
ENV MONGO_INITDB_ROOT_PASSWORD=password

# Optional: Copy initialization script to the container
COPY init-mongo.js /docker-entrypoint-initdb.d/
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,25 @@ This project requires Python 3.7 or later.


### Running the Server
To start FLask and FastAPI, run the given command:
To run the application and MongoDB Atlas locally using Docker, follow these steps:

1. Build and run the Docker container:
```bash
docker-compose up --build
```
This command builds the MongoDB container and runs it alongside your application.

2. To switch between the local Docker-based MongoDB Atlas and the cloud version, set the `USE_DOCKER_DB` environment variable in your application's environment:
- For using the Docker-based MongoDB Atlas, set `USE_DOCKER_DB=True`.
- For using the cloud version of MongoDB Atlas, set `USE_DOCKER_DB=False`.

3. To stop the Docker container, use the following command:
```bash
docker-compose down
```
This command stops and removes the containers, networks, and volumes created by `docker-compose up`.

To start Flask and FastAPI without Docker, run the given command:
```bash
python main.py
```
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.8'
services:
mongodb:
build: .
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
Loading