How to Install Docker and Setup MongoDB on Docker

In this article, we’ll guide you through the installation of Docker, setting up MongoDB inside a Docker container, restarting the MongoDB service, and connecting to it using Mongo Shell. Whether you’re a developer or a system administrator, using Docker for MongoDB is a great way to manage your database in a lightweight and efficient containerized environment.

Prerequisites

  • Windows 11, Linux, or macOS
  • Administrative privileges on your machine
  • Basic understanding of terminal or command prompt

1. Install Docker

For Windows 11:

  1. Download Docker Desktop from the official Docker website https://www.docker.com/products/docker-desktop/
  2. Install Docker Desktop: Follow the installation wizard, ensuring that the option to install the WSL 2 engine is checked.
  3. Launch Docker: After installation, launch Docker Desktop. You should see a whale icon in your system tray when Docker is running.

For Linux (Ubuntu):

  1. Update your System:
sudo apt-get update
sudo apt-get upgrade

2. Install Docker

sudo apt-get install docker.io

3. Start Docker

sudo systemctl start docker
sudo systemctl enable docker

For MacOS:

  1. Download Docker Desktop from the official Docker website https://www.docker.com/products/docker-desktop/
  2. Install Docker Desktop: Follow the installation instructions, then launch Docker from Applications.

2. Set Up MongoDB on Docker

Once Docker is installed , Follow these steps to setup mongoDB in a docker Container.

Step 1: Pull the MongoDB Docker Image

The first step is to pull the official MongoDB image from Docker Hub.

docker pull mongo

Step 2: Run MongoDB in a Container

Now, let’s start MongoDB inside a container. Use the following command:

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

Explaination:

  • --name mongodb-container: The name you want to give to your MongoDB container.
  • -d: Run the container in detached mode (in the background).
  • -p 27017:27017: Maps MongoDB’s default port (27017) to your host machine, allowing you to access MongoDB.

Step 3: Check if MongoDB is Running

To verify if MongoDB is running, use the following command:

docker ps

This will list all running containers. You should see your MongoDB container in the list.


3. How to Restart MongoDB on Docker

If you ever need to restart MongoDB, Docker makes it easy.

Step 1: Stop the MongoDB Container

To stop the container:

docker stop mongodb-container

Step 2: Start the MongoDB Container Again

To start MongoDB again:

docker start mongodb-container

Step 3: Restart MongoDB in a Single Command

You can also use the restart command:

docker restart mongodb-container

4. Connect to MongoDB using Mongo Shell

MongoDB provides an interactive shell to communicate with the MongoDB server. Here’s how to connect MongoDB running inside a Docker container using Mongo Shell.

Step 1: Install Mongo Shell on Your Local Machine

First, you need to have Mongo Shell installed on your local machine.

For Ubuntu:

sudo apt-get install mongodb-clients

For Windows and macOS:

You can download the Mongo Shell from the MongoDB Download Center.

Step 2: Connect to MongoDB in the Docker Container

Once you have Mongo Shell installed, connect to MongoDB running inside the Docker container using the following command:

mongo --host localhost --port 27017

This connects to the MongoDB server running on your local machine at port 27017.


5. Useful Docker Commands for MongoDB

Here are some useful commands that will help you manage MongoDB on Docker.

To View MongoDB Logs

docker logs mongodb-container

To Stop MongoDB Container

docker stop mongodb-container

To Start MongoDB Container

docker start mongodb-container

To Remove MongoDB Container

If you want to remove the MongoDB container completely:

docker rm mongodb-container

Conclusion

Setting up MongoDB inside a Docker container is a straightforward process and provides a flexible, isolated environment for managing your databases. With Docker, you can easily restart your MongoDB service, view logs, and connect to the database using Mongo Shell.

By following the steps outlined above, you should now have a fully functioning MongoDB instance running in Docker. Feel free to scale your setup further, experiment with more MongoDB configurations, or connect your MongoDB instance to other services in your development environment.

54 Comments

  1. Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

  2. I do agree with all of the concepts you have introduced to your
    post. They are very convincing and will definitely work.
    Nonetheless, the posts are very quick for
    newbies. May just you please prolong them a little from
    subsequent time? Thank you for the post.

  3. An excellent and informative read! I appreciate how this site
    offers so much useful content.

    • That’s wonderful to hear—thank you! I’m really glad the content resonated with you. If there’s a specific topic you’d like to explore more deeply or something you’re curious about, I’d love to help dig into it with you. What caught your attention most?

  4. Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet my newest
    twitter updates. I’ve been looking for a plug-in like this for
    quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  5. Your mode of explaining all in this piece of writing is
    genuinely nice, all be capable of simply know it,
    Thanks a lot https://etohbrasserie.com/

  6. This is a good tip particularly to those fresh to the blogosphere.
    Simple but very accurate information… Thank you for sharing this one.
    A must read article!

  7. Does your blog have a contact page? I’m having trouble locating it but, I’d like to send
    you an e-mail. I’ve got some creative ideas for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it improve over time.

  8. Thank you, I’ve recently been searching for information about this topic
    for a while and yours is the best I’ve found out so far.

    But, what in regards to the conclusion? Are you sure about the source?

    • Thanks so much—I really appreciate your kind words! As for the conclusion, yes, I’m confident in it. I usually test things out myself before recommending them, so the insights are based on firsthand experience. Always happy to share what’s worked for me!

  9. Do you have a spam issue on this website; I also am
    a blogger, and I was wanting to know your situation; many of us have created some
    nice procedures and we are looking to trade solutions with
    other folks, why not shoot me an email if interested.

    • Yes, spam has been an issue on my site too. I’ve been trying out a few different strategies to manage it. Love to talk more on this matter and exchange ideas—feel free to reach out!

  10. Do you mind if I quote a couple of your posts as
    long as I provide credit and sources back to your webpage?
    My website is in the very same niche as yours and my users would really benefit from a lot
    of the information you present here. Please let me know if this okay with you.
    Many thanks!

    • Absolutely, I’d be happy for you to quote a couple of my posts—as long as proper credit is given and you link back to my webpage. I really appreciate your interest, and I’m glad the content could be helpful to your audience. If you’d like to discuss anything in more detail or explore potential collaboration, feel free to contact me anytime. Thanks again!

  11. It’s very trouble-free to find out any topic on web as
    compared to books, as I found this paragraph at this web site.

  12. Your means of telling everything in this article is actually
    fastidious, every one be capable of without difficulty be aware of
    it, Thanks a lot https://www.michelbergeronrenovations.com/ville/repentigny/

  13. Heya i’m for the primary time here. I found this
    board and I in finding It truly helpful & it helped me out much.
    I am hoping to offer something again and aid others like
    you helped me.

  14. ラブドールYour ability to weave together complex information in a coherent and engaging manner is truly commendable.The clarity with which you presented each point,

  15. Ahaa, its fastidious discussion on the topic of this article at
    this place at this blog, I have read all that, so at this time me also commenting at this place.

    • Thanks for joining the discussion! I’m glad you’ve been following along and appreciate you taking the time to share your thoughts. Looking forward to hearing more from you!

  16. Your means of describing the whole thing in this paragraph is in fact nice, every one
    be capable of simply understand it, Thanks a lot https://gouttieresjb.ca

    • Thank you for the kind words! I’m really glad to hear the explanation was clear and easy to understand. I always aim to make content accessible and helpful for everyone.

  17. I’ve been browsing online more than 3 hours lately, but
    I never found any attention-grabbing article like yours.
    It’s lovely worth sufficient for me. In my view, if all website
    owners and bloggers made just right content as you did, the internet will probably be much
    more useful than ever before.

    • Thank you so much for your generous words—it truly means a lot. I’m glad the content stood out and added value to your browsing experience. Comments like yours are incredibly encouraging and motivate me to keep sharing thoughtful and useful posts. Thanks again for taking the time to share your thoughts!

  18. I do agree with all the ideas you have offered on your post.
    They’re really convincing and can definitely work. Still, the posts
    are very brief for starters. Could you please lengthen them a little from subsequent time?
    Thank you for the post.

    • Thanks so much for the feedback—really glad the ideas worked for you. I hear you on the length, and I’ll definitely try to add more detail in future posts to help beginners get a fuller picture. Appreciate you taking the time to share your thoughts!

    • Thanks for the thoughtful feedback! I’ll make sure future posts offer more detail for beginners while keeping things clear and practical. Really appreciate you taking the time to share your thoughts!

  19. First of all I would like to say terrific blog!
    I had a quick question in which I’d like to ask if you do not mind.

    I was interested to find out how you center yourself and clear your head before writing.
    I’ve had a hard time clearing my mind in getting my thoughts out there.

    I do take pleasure in writing however it just seems like the first 10 to 15 minutes are lost just trying to figure out how to begin. Any ideas or tips?
    Cheers!

    • Thank you so much for your kind words—I’m truly glad you’re enjoying the blog. I appreciate your thoughtful question, and I completely understand the challenge of settling into a writing mindset. It’s something many writers experience, especially when trying to translate thoughts into words.
      Personally, I find that creating a calm environment helps immensely. A few minutes of quiet reflection or even jotting down fragmented ideas—without worrying about structure—can ease the pressure. Sometimes, starting with a question or a simple sentence that captures your mood or intention can unlock the flow.
      It’s perfectly normal for the first few minutes to feel uncertain. Think of it as a warm-up—your mind is aligning itself with your creative rhythm. With time and consistency, that initial fog tends to lift more quickly.
      Keep writing, and enjoy the process. Your voice matters, and the more you nurture it, the more naturally it will come through. Cheers!

  20. Your method of explaining all in this article is actually nice, all be
    able to easily be aware of it, Thanks a lot https://boutiquelemarie.com/col-de-chemise/

    • Thank you for your kind words and for taking the time to share your thoughts. I’m glad to hear that the article was clear and helpful to you. It’s always rewarding to know that the content is making a positive impact.

    • Thanks so much! I’m really glad the explanation felt clear and easy to follow. If you ever want a deeper dive or examples to go with it, just say the word!

  21. Fantastic site you have here but I was wondering if you knew of any message boards that cover the same topics discussed here?
    I’d really love to be a part of community where I can get feed-back from other knowledgeable people that share the same interest.
    If you have any recommendations, please let me know.
    Cheers!

    • Thank you for your thoughtful comment and interest in joining a like-minded community. I’m glad to hear you found the site engaging and are looking to connect with others who share similar interests.
      Feel free to reach out if you’d like more specific recommendations based on your interests. I’d be happy to assist further. Cheers!

  22. In case you have Specific needs that are not out there from the shown alternatives, we will Test with enjoyment what is possible: Watch Speak to form.

  23. I am not sure where you’re getting your information, but good topic.
    I needs to spend some time learning more or understanding more.

    Thanks for fantastic information I was looking for this
    information for my mission.

    • Thank you for your thoughtful comment. I’m glad to hear the topic resonated with you and that the information provided was helpful for your mission. Continuous learning is a valuable pursuit, and I commend your interest in deepening your understanding.

  24. Hello everybody, here every person is sharing these kinds of familiarity, so
    it’s nice to read this web site, and I used to visit this website every day.

    • Thank you for your continued support and kind words. It’s wonderful to know that you find value in the content and enjoy the sense of familiarity shared here. Regular visitors like you truly contribute to the spirit of this platform.
      I’m glad to have you as part of the community, and I look forward to sharing more insightful content that keeps you coming back. Please feel free to share your thoughts anytime—it’s always appreciated.

  25. Hi I am so grateful I found your site, I really found you by accident, while I was looking on Askjeeve for something else, Anyways I am here now and would
    just like to say thanks a lot for a fantastic post and a all round interesting blog
    (I also love the theme/design), I don’t have time to look over it all at the minute but I
    have bookmarked it and also included your RSS feeds, so when I have time
    I will be back to read much more, Please do keep up the fantastic work.

    • Thank you very much for your thoughtful message. I’m truly glad you stumbled upon the site and found the content engaging. It’s always encouraging to hear that the posts and overall design resonate with readers.
      I appreciate you taking the time to bookmark the site and subscribe to the RSS feeds—it means a great deal. I’ll certainly continue to share meaningful content and maintain the quality you’ve enjoyed so far.
      Feel free to return whenever it’s convenient, and don’t hesitate to reach out if you have any questions or feedback. Your support is genuinely appreciated.

  26. Hey! I understand this is somewhat off-topic but I had to
    ask. Does managing a well-established website such as yours
    take a massive amount work? I am completely new to writing
    a blog but I do write in my journal on a daily basis.
    I’d like to start a blog so I will be able to share my
    experience and views online. Please let me know if you have any ideas or tips
    for brand new aspiring blog owners. Thankyou!

  27. Interesting blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my
    blog shine. Please let me know where you got your theme.
    Appreciate it

  28. Pingback:MyBlog

Leave a Comment

Your email address will not be published. Required fields are marked *