Follow the below commands to set up MongoDB on Docker Container
Step 1: Pull MongoDB Docker Imagesudo docker pull mongo:latest
Step 2: Create local directory to map MongoDB data directory
sudo mkdir -p /home/dmuser/mongodb_data
Step 3: Run MongoDB Docker Image as Docker Container
sudo docker run -d -p 27017:27017 -v /home/dmuser/mongodb_data:/data/db --name demo-mongodb mongo:latest
Step 4: Login to demo-mongodb(Docker Container) on bash mode
sudo docker exec -it demo-mongodb bash
Step 5: Open mongo CLI(Command Line Interface)
mongo
Step 6: Create database in MongoDB
use test_db;
Step 7: Create collection in MongoDB database
db.createCollection("user_dtl");
Step 8: Insert sample documents to MongoDB collection
db.user_dtl.insert({'user_id': 1, 'user_name': 'Arun', 'city': 'Chennai'})
db.user_dtl.insert({'user_id': 2, 'user_name': 'Ram', 'city': 'Bangalore'})
db.user_dtl.insert({'user_id': 3, 'user_name': 'Shiva', 'city': 'Hyderabad'})
Step 9: Get 2 documents from MongoDB collection
db.user_dtl.find().limit(2).pretty();
Step 10: Get documents count of MongoDB collection
db.user_dtl.countDocuments({})
Click Here for Video Tutorial
0 Comments