Skip to content

Instantly share code, notes, and snippets.

@craig-m-unsw
Last active August 30, 2022 09:05
Show Gist options
  • Save craig-m-unsw/4057ce9c7407622a41fb0edd8e2c0a5d to your computer and use it in GitHub Desktop.
Save craig-m-unsw/4057ce9c7407622a41fb0edd8e2c0a5d to your computer and use it in GitHub Desktop.
Learn MongoDB with docker-compose

Learn MongoDB with docker-compose

A Docker compose to learn about MongoDB noSQL database.

With MongoExpress webUI (written with Node.js and express).

Docs:

git clone https://gist.github.com/craig-m-unsw/4057ce9c7407622a41fb0edd8e2c0a5d mongodb-learn
mongodb-learn/
      ├── docker-compose.yml
      └── README.md

setup

Start the DB.

docker-compose up -d
docker ps

setup - vagrant

Vagrant VM:

vagrant init -m generic/ubuntu2004
vagrant up
vagrant ssh --command "sudo apt-get update && sudo apt-get install -y -q docker.io docker-compose"
vagrant ssh --command "sudo gpasswd -a vagrant docker"
scp -F /dev/stdin < <(vagrant ssh-config) -v *.{yml,yaml,md} vagrant@default:/home/vagrant/
vagrant ssh -- -L 8081:127.0.0.1:8081 -t tmux
docker-compose up -d
docker-compose logs -f

using

Login to http://localhost:8081 for the WebUI.

No username or pass is required.

Login to Mongo DB shell:

dbid=$(docker ps | grep -e 'mongo' | grep -v 'express' | awk '{ print $1 }');
docker exec -it ${dbid} bash;
mongosh --username root --password rootpass mongodb://mongodb:27017/

Show current DB

test> db

Show databases:

test> show dbs

Get stats on current DB:

test> db.stats()

collections

Create a collection:

test> db.createCollection('foobar')

List all collections:

test> db.getCollectionNames()
version: '3.1'
services:
# https://hub.docker.com/_/mongo
mongodb:
image: mongo
restart: always
ports:
- 127.0.0.1:27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpass
volumes:
- mongodata:/data/db
networks:
- mongonet
# https://hub.docker.com/_/mongo-express
mongo-express:
image: mongo-express
restart: always
ports:
- 127.0.0.1:8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: rootpass
ME_CONFIG_MONGODB_URL: mongodb://root:rootpass@mongodb:27017/
networks:
- mongonet
networks:
mongonet:
volumes:
mongodata:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment