Run MongoDB in local environment using Docker

Running MongoDB in local environment using Docker without installing the MongoDB server would be my preference since I use Docker. The reason is obvious, I don’t want to clutter my development environment with a bunch of database server installations.

With Docker installed on development environment I could just simply execute the following command to spin up a new MongoDB server running in a container:

docker run -d -p 27017:27017 –name=local-mongodb mongo:3.6

But, still to connect with the database you would need a client, in my case, I simply installed the mongodb-org-shell and use it to connect to the MongoDB database running in the container. The earlier command I showed to you will run the MongoDB container and expose the 27017 port to the host machine. Connecting to the MongoDB in the container would simply just executing the following command on the CLI:

mongo

I use this approach in several projects, and I am quite happy.

Advertisements

Keeping my local development environment clean with Docker

Sometimes managing my development environment would be a time consuming as there is so many things to setup. Lets take databases for example. There is so many databases I can try like MongoDB, MySQL, Cassandra, Postgre, Couchbase and Redis. What if I am interested to try out all of those databases. I will probably ended up installed those databases with so many configurations to deal with. To simplify things I use Docker.

Instead of installing the database directly into my host, I can just pull the database images hosted on Docker Hub and run it as a docker containers.

Screenshot from 2015-12-30 23:38:58

Some of database images installed on my local host

If you guys interested to use Docker, I suggest you to read more about it start from here: Understand the architecture.

Between Me, Docker client and Docker host

After reading the Docker documentation, Understand the architecture. This is how I draw my relationship between me, Docker client and Docker host.

This picture illustrating my relationship between me, Docker client and Docker host.

This picture illustrating my relationship between me, Docker client and Docker host.

Interesting things that I noticed so far is when the docker client is building the image and send it to docker host on my laptop, it only takes a minutes or so, but when docker client is building the image and send ti docker host that is located somewhere out there (e.g. machine that hosted on cloud providers) it takes a longer time.