DEV Community

Cover image for MongoDB With Docker
Kishor Kc
Kishor Kc

Posted on

MongoDB With Docker

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Run the first MongoDB container:

docker run -d --name mongo-one -p 27017:27017 mongo
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

execute a running Docker container: mongo-one

docker exec -it mongo-one bash
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>';
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Feel free to use the help command if you find yourself confused or uncertain about how to proceed.

help
Enter fullscreen mode Exit fullscreen mode

switch to the store database

use store
Enter fullscreen mode Exit fullscreen mode

Get all the collections present in the currently selected database

show collections
Enter fullscreen mode Exit fullscreen mode

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"}
])
Enter fullscreen mode Exit fullscreen mode

Get all the fruit

db.fruit.find()
Enter fullscreen mode Exit fullscreen mode

Get all the fruits which name contain banana

db.fruit.find({name: "banana"})
Enter fullscreen mode Exit fullscreen mode

Get the single fruit by name

db.fruit.findOne({name: "banana"})
Enter fullscreen mode Exit fullscreen mode

Updates a single document based on the matching name

db.fruit.findOneAndUpdate(
   { name: "banana" },
   { $set: { price : 100 } },
)
Enter fullscreen mode Exit fullscreen mode

Resources

Thank you.
Remember to keep learning and exploring new things.

Top comments (0)