MongoDb, Docker and Compass

The other day I struggled a bit in setting up MongoDb in a docker container and then connecting it with Compass – the MongoDb client. That’s when I thought I would document the knowledge here so that anyone else who run into similar problems could look into it (and also in case I forget later, I could always come back to check myself).

Docker is a tool that allows you to run and manage an application using containers. So what exactly is a container? Why not Virtual Machine? Virtual machines have this huge overload of virtualizing the hardware. In contrast, the containers are lightweight and virtualize the OS. Consider it as a standard unit of software that packages the application and all dependencies in a package, executing them regardless of the underlying configuration or hardware.

A gist of Docker and various concepts related to it are highlighted in the image below.

To start running a MongoDb instance in a container, you could use the following command.

docker run --name containerName -d -p 27017:27018 mongo

Where containerName is the name you would like to assign to the container instance. The -p arguement and the number that follows allow you to specify the port for access. This is important for accessing the MongoDb Server instance from an application running locally, like the Compass Client.

Remember to specify the port – This is the step that I missed and couldn’t connect to the Server. Once that is done, you can now connect the Compass Client using the following connection string.


That’s all – You have your MongoDb instance running in your container. Cheers.


One thought on “MongoDb, Docker and Compass

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s