This repository contains a simple setup for running a MongoDB instance using Docker and Docker Compose. It also includes a basic Python script to test the connection to the MongoDB server.
Before you begin, ensure you have the following installed on your system:
Follow these steps to get your MongoDB server running:
Clone this repository to your local machine:
git clone https://github.com/jhon0010/python-examples.git
cd python-examples
From the root of the cloned directory, run:
docker-compose up -d
This command starts a MongoDB instance in a Docker container. The -d flag runs the container in detached mode.
Check if the MongoDB container is running:
docker ps
You should see the MongoDB container listed.
Run the Python script to test the connection:
python mongo_connect.py
If the connection is successful, you'll see a message indicating that you're connected to MongoDB.
To stop the MongoDB Docker container, run:
docker-compose down
-
Set up a virtual environment (optional but recommended):
- Create a new directory for your project.
- Open a terminal or command prompt and navigate to the project directory.
- Create a virtual environment using the following command:
python3 -m venv myenv
- Activate the virtual environment:
- On macOS and Linux:
source myenv/bin/activate
- On Windows:
myenv\Scripts\activate
- On macOS and Linux:
-
Install all the required dependencies:
- While inside the virtual environment, install the requirements using pip:
pip install -r requirements.txt
- While inside the virtual environment, install the requirements using pip:
Using uvicorn to run the file app with the module app in the port 8000.
The reload flag is used to reload the server automatically when the code is changed.
uvicorn app:app --reload
Go to the test folder and execute the command:
pytest
Go to the url http://localhost:8000/docs
The Dockerfile and docker-compose.yml files are configured to use the default MongoDB port 27017.