December 22, 2024

Run MongoDB and MongoAdmin in Docker

In this post I’m going to provide a simple example of how MongoDB with adminMongo can be run on your local machine with minimum effort. This can be useful in development scenarios or simply if you want to test something. 

What is MongoDB?

MongoDB is a NoSQL database aimed for both big and small applications designed with scalability in mind. Instead of saving tabular data, MongoDB stores data as collections and documents.

What is adminMongo?

adminMongo is simply a web interface for data query, save or delete. There are a lot of Mongo administration tools out there but I chose this one for its simplicity. 

Building adminMongo with Docker

Let’s go straight to business. Before running both of them with docker compose, we need to build a new image based on original adminMongo image that changes the container configuration. We need this because:

  1. adminMongo listens to localhost by default and we need to make it listen to all interfaces, so we will make it listen to 0.0.0.0
  2. We want to password-protect the access to web interface

This is the app.config file for configuring MongoAdmin:

{
    "app": {
        "host": "0.0.0.0",
        "port": 1234,
        "password": "1q2w3e",
        "locale": "en"
    }
}

And the Docker file is very simple:

FROM mrvautin/adminmongo
COPY app.json /app/user/config

This is enough to build our adminMongo service for now.

Writing the compose file

The next step is to write a compose file that will bring up the services. The configuration file will create a MongoDB and adminMongo container and will expose port 1234 to our system.

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-admin:
    build:
      context: ./mongo-admin
    restart: always
    ports:
      - 1234:1234
    environment:
      DB_USERNAME: root
      DB_PASSWORD: example

Running the whole setup

It’s now time to run the compose file. If you run ‘docker compose up’ in the folder where the compose file is located, you should be able to see the following output:

mongo-admin_1  | adminMongo listening on host: http://0.0.0.0:1234/dbApp
mongo_1        | MongoDB init process complete; ready for start up.

You can now go to http://localhost:1234/dbApp/app/connection_list to define a connection. Give a name to the connection and use the following connection string: ‘mongodb://root:example@mongo’.


You should be able now to connect to the database

From now on you can play around by querying or creating data. Hope you find this to be a useful toy in your playground! ðŸ˜ƒ

I almost forget to upload the code!

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. If you found my content useful please consider a small donation. Any support is greatly appreciated! Cheers  😉

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

View all posts by afivan →