How to create your own Mongo DB server (dockerized)?

Hi there, in today's blog post, we will go over how you can set us your own mongoDB instance using Docker!

What you will need

  1. A linux machine
  2. Docker installed on your machine. If you want to install docker - follow this guide.

Get started

We will deploy mongoDB as a docker container and mount the data directory to this container as a persistent volume. In addition, we will run a container called mongo-express which is a lightweight UI interface that you can use in order to manage your mongo database. Both these services will be deployed using docker stacks so that we can ensure high availability.

Copy over the docker-compose file below to a directory of your choice.

#docker-compose.yml
version: '3.1'

services:

  mongo:
    image: mongo:5.0.3
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: meraki
      MONGO_INITDB_ROOT_PASSWORD: meraki123
    volumes:
      - /mnt/databases/scrum_data_mongodb:/data/db
    ports:
      - 27017:27017

  mongo-express:
    image: mongo-express:0.54
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: meraki
      ME_CONFIG_MONGODB_ADMINPASSWORD: meraki123
      ME_CONFIG_MONGODB_URL: mongodb://meraki:meraki123@scrum_mongo:27017/

Note: Notice that we mount the volume /mnt/databases/scrum_data_mongodb to be the data directory. You can choose a directory of your choice.
We expose the ports 27017 and 8081 of the local host to the container ports 27017 and 8081 respectively. This is so that you can access the service that is deployed using the hostname of the machine on which this container is deployed. You can map any other port on the host machine to the container ports 27017 and 8081 if you choose.

Deploy the service

Deploy the docker service using the following command. In this example, I call the service scrum_mongo. You can choose whatever name you would like.

docker stack deploy -c docker-compose.yml scrum_mongo

Ensuring things work

To ensure that your service is up and running, you can use the following commands:

mukul@test-machine:~/scrum-mongo$ docker stack ps scrum_mongo
ID             NAME                          IMAGE                NODE          DESIRED STATE   CURRENT STATE        ERROR     PORTS
vhaszhpjfjq2   scrum_mongo_mongo-express.1   mongo-express:0.54   test-machine   Running         Running 3 days ago
ovpucxq1waiq   scrum_mongo_mongo.1           mongo:5.0.3          test-machine   Running         Running 3 days ago

mukul@test-machine:~/scrum-mongo$ docker service ls
ID             NAME                        MODE         REPLICAS   IMAGE                       PORTS
pe4icv7rzjdn   scrum_mongo_mongo           replicated   1/1        mongo:5.0.3                 *:27017->27017/tcp
iiw4voluuodf   scrum_mongo_mongo-express   replicated   1/1        mongo-express:0.54          *:8081->8081/tcp

Access the UI on your browser using http://[IP of local machine / localhost]:8081 to see the stats of your database.

mongo_preview

And that's it! You can now use this database for your projects!

Previous
Previous

Understanding Jenkins Pipelines - Part 1

Next
Next

Kubernetes ReplicaSets