Skip to content
Araz Shah
Menu
  • Home
  • About me
  • Contact me
  • CV
  • Online Courses
    • Apply Now !
    • In-Depth
    • Courses
      • Concepts
      • Python Course
      • GIS Developer Course
    • Price
Menu

Configuring Docker with MongoDB and connecting it with PyMongo

Posted on August 18, 2023 by admin

Configuring Docker with MongoDB and connecting it with PyMongo involves several steps. Docker allows you to create isolated environments (containers) for applications, and PyMongo is a Python driver for MongoDB. Here’s a step-by-step guide:

Step 1: Install Docker
If you haven’t already, install Docker on your machine. You can download it from the official Docker website: https://www.docker.com/get-started

Step 2: Pull MongoDB Image
Open a terminal and run the following command to pull the official MongoDB Docker image:

docker pull mongo

Step 3: Run MongoDB Container
Now, let’s run a MongoDB container using the pulled image:

docker run -d --name mongodb-container -p 27017:27017 mongo

Here, -d runs the container in detached mode, --name gives the container a name, and -p maps the container’s port 27017 to the host’s port 27017.

Step 4: Install PyMongo
Install the PyMongo library in your Python environment. You can do this using pip:

pip install pymongo

Step 5: Connect to MongoDB in Python
Now that you have the MongoDB container running and PyMongo installed, you can connect to the MongoDB instance from your Python script. Here’s an example script:

from pymongo import MongoClient

# Create a MongoDB client
client = MongoClient('localhost', 27017)

# Access the desired database
db = client['mydatabase']

# Access the desired collection
collection = db['mycollection']

# Insert a document
document = {'name': 'John', 'age': 30}
collection.insert_one(document)

# Find documents
documents = collection.find({'age': {'$gt': 25}})
for doc in documents:
    print(doc)

# Close the client connection
client.close()

Replace 'mydatabase' and 'mycollection' with the actual names of your database and collection.

Remember to handle exceptions, use connection pooling, and manage your resources properly in a real-world application.

Step 6: Clean Up
When you’re done, you can stop and remove the MongoDB container:

docker stop mongodb-container
docker rm mongodb-container

That’s it! You’ve successfully configured Docker with MongoDB and connected it with PyMongo. This setup allows you to develop and test applications without affecting your local environment and database installations.

Category: python, Tutorials

Post navigation

← Second Step Git Repository Configure
user login checks using MongoDB and Python →

Recent Posts

  • Geospatial Risk Assessment: A Python Approach
  • Analyzing Employee Arrival Patterns and Delays Using Geospatial Data
  • Real-Time GPS Tracking on a Web Map using FastAPI & Leaflet
  • How to Create a Simple WebGIS with FastAPI, PostGIS, and Leaflet.js
  • Graph Coloring: How Many Colors Do You Need?

Archives

  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023

Categories

  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023
  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos

Araz Shahkarami

I’m a software enthusiast with a deep love for crafting robust and efficient solutions. My journey into the world of programming began several years ago when I was introduced to the world of code. Since then, I’ve been on an exhilarating ride of learning, problem-solving, and continuous improvement.

© 2025 Araz Shah | Powered by Minimalist Blog WordPress Theme