Dockerize mongodb

(λx.x)eranga
Effectz.AI
Published in
2 min readDec 3, 2015

Docker file

Following is the docker file that I have created to dockerize mongo db.

Few things to notice here

1. Create data directory

MongoDB requires a data directory. Data directory defines from which directory MongoDB reads and writes it’s data. Our data directory on container is /data/db. The VOLUME instruction will tells the container that /data/db is a designated mount point for external hosts or other containers. While running container of mobgodb we can map this volume to a folder in our local host machine(so data will be available to our local machine).

2. Expose running port

We have to specify the mongodb running port. EXPOSE instructs the container to expose the given port to the host system. We are exposing 27017 port. We can connect to mogodb via this port.

Build docker

docker build — tag senz/mongodb .

This command will build a docker image with a tag senz/mogodb

Run mongodb

docker run \
-name mongo-dev \
-d \
-v /opt/mongodb:/data/db \
-p 27017:27017 \
senz/mongodb

In here I’m mapping data directory of mongo db(/data/db) to the /opt/mongodb in my host machine(local machine).

This will create a mongo db container with name mongo-dev

Mongodb on command line

In order to access mongodb from command line you have to install mogodb client. This client comes with mongndb installation. In mac I have used homebrew to install it.

brew install mongodb

Then you can connect to mogodb docker instance. In here I’m running docker with boot2docker vm. So my mongo db docker container runs on boot2docker vm. I can connect to it via

mongo <host>:<port>

Host is my boot2docker host(192.168.59.103). Port is the docker container exposed port(27017). Following is the command to run it on my machine

mongo 192.168.59.103:27017

--

--