Developing with Docker

What is MongoDB?

MongoDB is an open-source, cross-platform, and distributed document-based database designed for ease of application development and scaling. It is a NoSQL database developed by MongoDB Inc.

MongoDB is not a Relational Database Management System (RDBMS). It's called a “NoSQL” database. It is opposite to SQL based databases where it does not normalize data under schemas and tables where every table has a fixed structure. Instead, it stores data in the collections as JSON based documents and does not enforce schemas. It does not have tables, rows, and columns as other SQL (RDBMS) databases.

The following table lists the relation between MongoDB and RDBMS terminologies.

MongoDB (NoSQL Database)RDBMS (SQL Server, Oracle, etc.)

Database

Database

Collection

Table

Document

Row (Record)

Field

Column

In the RDBMS database, a table can have multiple rows and columns. Similarly, in MongoDB, a collection can have multiple documents which are equivalent to the rows. Each document has multiple “fields” which are equivalent to the columns. Documents in a single collection can have different fields.

The following is an example of JSON based document.

What is MongoExpress?

Mongo Express is an interactive lightweight Web-Based Administrative Tool to effectively manage MongoDB Databases. Written with Node.js, Express, and Bootstrap3, MongoExpress can be used to simplify several MongoDB Admin tasks. Using Mongo Express, you can add, delete or modify databases, collections, and documents.

Installing a MongoDB Docker Container

AsafsMacBook:~ aahmadov$ docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
d4ba87bb7858: Already exists 
cd0512bdaae7: Already exists 
51571d358e42: Already exists 
aa226bdd9830: Already exists 
8c0e3e44d3ac: Already exists 
e106719865e3: Already exists 
600ad43e7428: Already exists 
943041bc9b73: Pull complete 
b2c0dd883907: Pull complete 
Digest: sha256:d8f48972427a3d62bbb7e953bd5afb609ab6e9b69f347c01c7cfda133cf15b9c
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

Installing a MongoExpress Docker Container

AsafsMacBook:~ aahmadov$ docker pull mongo-express
Using default tag: latest
latest: Pulling from library/mongo-express
Digest: sha256:2a25aafdf23296823b06bc9a0a2af2656971262041b8dbf11b40444804fdc104
Status: Image is up to date for mongo-express:latest
docker.io/library/mongo-express:latest

Docker Network

Starting the application with Docker

✅ Step 1: Create docker network

AsafsMacBook:~ aahmadov$ docker network create mongo-network
448e11cc559f5beed079422ca0df12b346b4a062ec34bfafb34e8083acb1bb06
AsafsMacBook:~ aahmadov$ docker network ls
NETWORK ID     NAME            DRIVER    SCOPE
cf742d55818b   bridge          bridge    local
1124f0b140fb   host            host      local
448e11cc559f   mongo-network   bridge    local
56a06b76d051   none            null      local
1c8f394d2e40   test_nw         bridge    local

✅ Step 2: start mongodb

docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=test --name mongodb --net mongo-network mongo    

✅ Step 3: start mongo-express

docker run -d -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=test --net mongo-network --name mongo-express -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express   

NOTE: creating docker-network in optional. You can start both containers in a default network. In this case, just emit --net flag in docker run command

✅ Step 4: open mongo-express from browser

http://localhost:8081

✅ Step 5: create my-db db and users collection in mongo-express

✅ Step 6: Start your nodejs application locally - go to app directory of project

cd app
npm install 
node server.js

Step 7: Access you nodejs application UI from browser

http://localhost:3000

Last updated