MongoDB-QuickSetup

4 minute read

Published:

About MongoDB

MongoDB is a persistent data storage solution where users/applications store data. It falls under NoSQL category i.e. instead of traditional table format used by SQL databases it follows JSON notation to store data. It provides more felxibility than SQL based databases. So be it user data, images, binaries, one can store almost anything is MongoDB.

This tutorial is a quick guide to setup a MongoDB instance.

Either one can install the MongoDB binaries locally, which obviously takes a little more time and might get conflict with any existing MongoDB installation, or, one can avoid all that mess by starting MongoDB instance inside a docker container which takes less time and easier to maintain. So, let’s learn how to deploy a MongoDB container in less than 2 min.

Step 1 - Install Docker

It is highly recommended to install docker from the official Docker website.

Also, docker-compose is needed in order to make sure you are doing things neat and earier to debug in case of an error. Install docker-compose.

Step 2 - Create an installation directory

It is a good practise to create a directory where everything related to the MongoDB you are about to use should be put.

$ mkdir mongo

# create a volume to store the MongoDB data from container
$ mkdir mongodb_data

# now enter the created directory
$ cd mongo

Create docker-compose file:

To quickly setup a MongoDB instance inside a container there are a few things you need to take under consideration:

  • Requiring user authentication or not?
  • Attach a local volume to get the data from inside your MongoDB container?
  • Choosing appropriate port, standard port is 27017 but you can choose your own as well

Code to start the MongoDB container should be put inside a docker-compose.yml file.

Assume you want authentication based MongoDB access running on port number 27017 with a volume on the host machine to get the MongoDB data, you can use the following template:

version: '3.8'

volumes:
        mongodb_data:
services:
      mongodb:
        image: mongo
        container_name: mongodb
        restart: always
        volumes:
                - 'mongodb_data:/data/db'
        environment:
          MONGO_INITDB_ROOT_USERNAME: 'superuser'
          MONGO_INITDB_ROOT_PASSWORD: 'password'
          MONGO_INITDB_DATABASE: mongodb
        ports:
          - 27017:27017

In the snippet above you can choose replace the environment variables such as username, password, database etc. with the credentials of your choice.

If you want a MongoDB instance without autentication requirements, simple remove the environment variable and 3 following lines under that. Now new code will look like this:

version: '3.8'

volumes:
        mongodb_data:
services:
      mongodb:
        image: mongo
        container_name: mongodb
        restart: always
        volumes:
                - 'mongodb_data:/data/db'
        ports:
          - 27017:27017

Step 3- Deploying MongoDB container

To deploy the container containing the MongoDb instance, use the following command:

$ docker-compose up -d --build
Creating network "mongo_default" with the default driver
Creating mongodb ... done

This will spawn a docker container running MongoDB service which can be accessed by the user. To verify if the container is up and running user the following command:

$ docker ps

If you see output something like the follwoing then it means the MongoDB container is up and running:

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
4bb7211b38b1   mongo     "docker-entrypoint.s…"   8 minutes ago   Up 8 minutes   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   mongodb

If you don’t see such output then something went wrong while deploying the container. If you see any error then, Well in that case probably google it or ask me, I will be happy to help!

Step 4 - Stoping the MongoDB container

To stop the container use the following command from the same directory where the original docker-compose.yml file is present.

$ docker-compose down -v

Note using the command above will not only stop the MongoDB container but will also remove it from the system so MongoDB specific logs which shows what was going inside the MongoDB container will be inaccessible. BUT It will not remove any data generated by MongoDB from your mongodb_data directory.

Step 5- Debugging the MongoDB container

If you want to see what is going inside the container then you can use the docker logs feature. To use it following command will help:

# after logs it is the container id, you will get from the `docker ps` command as mentioned above
$ docker logs 4bb7211b38b1

docker logs command can also be used on stopped containers, you just need to get its container id, by using docker ps -a command if the container was not removed manually or using the docker-compose down command as mentioned in the previous section.

Using your application

Now you can use any application that require a MongoDB instance running on port 27017 like it was installed on your local machine. The experience will be very smooth and easier to manage.

happy hacking!