Containerizing — How to make your mongodb database as docker container

Ari Nurcahya
2 min readJan 19, 2021

Hello guys, Last night I got the problem when I want to fixing the bug for my project with my friend by the way the project is build an api for recommendation using fuzzy-ahp and it built on top of nodejs. well some problem always facing to me since I had been updated my os to bigsur, like I don’t know why my mongodb instantly won’t be running when I typed “mongod” from my terminal. the response of mongod says “successfully running” but when I typed “mongo” to connect to the mongo instance well it won’t run. then I tried to type netstat from my terminal

netstat -an | grep 27017

it shows nothing. after this problem facing to me I deciding to repairing it, but it make my brain exploding. and after that I remember, I don’t have any database in mongo that importance for my life. so I just uninstalled my mongodb from my machine and use docker.

firstly, You must installed docker on your machine. If you have been installed docker on your machine continue to this step.

type this code

docker run \
-d \
--name=mongodb \
-p 27017:27017 \
--volume=/Users/nurcahyaari/Documents/docker-volumes/mongodb-dir:/data/db \
mongo

what is that?

first we type docker run, that notice we want to running a docker from any images.

then we used -d mode or detach mode, it means the container will run in background process.

— name means the name for our container, here I named it as mongodb

-p means the port that will exposed to our machine, if we don’t exposed the port the container would not callable from our machine, and only callable from any container.

— volume means we want to make our data at our container are persistance.

and last mongo is a images that we’ll use for our container

if you successfully create a new container it now accessible from our machine, you can tried to connect to the mongodb with this command

docker exec -it mongodb mongo

and it will show like this

--

--