DEV Community

Cover image for MongoDB Setup With Docker
redeemefy
redeemefy

Posted on

MongoDB Setup With Docker

I'm not in the habit of writing my findings or steps when solving a problem. I am now making myself documenting as much as I can even though the solution is simple. I'm doing it since I have found myself searching the internet for things I worked on before but are not day-to-day tasks. I'm hoping on documenting things often enough that I can find them myself and potentially someone else can benefit from it.

If you are like me, you don't like to install apps/tools on your machine just to try out an idea. Also, the truth goes when you are developing. You might want to keep your dev and prod environments the same throughout the SDLCs for avoiding bugs and/or problems. What we are actually trying to avoid is the famous phrase "It works in my computer...". Thank you, Docker, that you help us to avoid precisely that.

Let us enable MongoDB in our machine with Docker.

  • mkdir ~/docker-data && mkdir ~/docker-data/mongodb
  • docker run --name mongodb -v ~/docker-data/mongodb:/data/db -p 27017:27017 -d mongo

The first command creates a directory for mounting a volume where your data can be persisted, when you resume development the next day your data would be there. You can choose a different location though. The second command creates/downloads your image, exposes a port, mounts the volume, and starts the container.

  • docker ps to see running containers
  • docker ps --all to see all containers
  • docker stop mongodb to stop the mongodb container
  • docker start mongodb to start the mongodb container

Top comments (0)