I am explaining how Docker can be utilized to set up the database and demonstrate its practical applications. In this article, I am focusing on utilizing the MongoDB database and provide instructions on its utilization.
There are several benefits to setting up MongoDB with Docker instead of installing it directly on your local PC:
Isolation
: Docker keeps MongoDB separate from your local system, ensuring stability and consistency.Deployment
: Docker makes it easy to deploy multiple MongoDB instances and manage replica sets or clusters.Consistency
: Docker ensures consistent environments across different machines, avoiding version or configuration conflicts.Cleanup and Portability
: Docker allows easy removal of MongoDB containers and seamless migration to different environments.Dependency Management
: Docker simplifies managing MongoDB versions and dependencies.
I'm ready to help you install MongoDB in a Docker container. Let's get started!
To install mongo in docker
docker pull mongo
To determine if MongoDB is installed within our Docker environment, we need to check whether the Mongo image is present. If the image is found, we can view its ID, tag/version, and size. However, if the image is not available, no information will be displayed.
docker images mongo
Run the first MongoDB container:
docker run -d --name mongo-one -p 27017:27017 mongo
Run the Second MongoDB container:
If you need the multiple container, then you can use it
docker run -d --name mongo-two -p 27018:27017 mongo
In this above example, the container are mongo-one and mongo-two, and the -p option maps port 27017/27018 from the host machine to the container. You can choose a different container name and port mapping if desired.
Show all currently running Docker containers
docker ps
execute a running Docker container: mongo-one
docker exec -it mongo-one bash
after execute a running docker container of mongo, we have to start the MongoDB Shell and interact with the MongoDB database running inside the container
mongosh
Once the MongoDB container is successfully executed, you will obtain the database URL, which can be used with various programming languages such as Node.js, Python, Rust, Go, and more. This URL will enable you to establish a connection to the MongoDB database from your chosen programming environment.
const DB_URL1='mongodb://localhost:27017/<db_name>';
const DB_URL2='mongodb://localhost:27018/<db_name>';
Both DB_URL1 and DB_URL2 are database URLs that can be obtained when running the Docker containers for MongoDB. If you execute the "mongo-one" and "mongo-two" containers as mentioned, both DB_URL1 and DB_URL2 will be operational and can be used to establish connections to the respective MongoDB databases.
Once the database is up and running, I can share a set of commands through the Docker shell to interact with MongoDB. These commands enable us to perform various operations and manipulate the MongoDB database within the Docker environment
show all the databases
show dbs
Feel free to use the help
command if you find yourself confused or uncertain about how to proceed.
help
switch to the store
database
use store
Get all the collections present in the currently selected database
show collections
Create multiple fruit in the store
db.fruit.insertMany([
{name:"apple", price: 250, color: "red"},
{name:"banana", price: 120, color: "yellow"},
{name:"papaya", price: 150, color: "yellowish-orange"}
])
Get all the fruit
db.fruit.find()
Get all the fruits which name
contain banana
db.fruit.find({name: "banana"})
Get the single fruit by name
db.fruit.findOne({name: "banana"})
Updates a single document based on the matching name
db.fruit.findOneAndUpdate(
{ name: "banana" },
{ $set: { price : 100 } },
)
Resources
Thank you.
Remember to keep learning and exploring new things.
Top comments (0)