Docker MongoDB DataBase


$ mkdir -p ~/projects/dev/db/mongo/
$ cd ~/projects/dev/db/mongo/


$ vi docker-compose.yml


version: '3'
services:
  mongodb-dev:
    image: mongo
    restart: always
    ports:
      - '27017:27017'
    environment:
      MONGODB_DATABASE: mongo-database


$ docker-compose up


Example: https://github.com/webmakaka/NestJS-Fundamentals-Course/blob/main/app/api-mongodb/docker-compose.yml


Another version with authentication


$ vi docker-compose.yml


version: '3'
services:
  mongodb:
    image: mongo:4.4.6
    container_name: mongo
    restart: always
    ports:
      - ${MONGO_PORT}:27017
    environment:
      - MONGODB_DATABASE=${MONGO_DATABASE}
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_LOGIN}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD}
    volumes:
      - ./mongo-data-4.4:/data/db
    command: --wiredTigerCacheSizeGB 1.5


.env

MONGO_HOST=localhost
MONGO_DATABASE=mongo-database

MONGO_LOGIN=admin
MONGO_PASSWORD=admin
MONGO_PORT=27017


$ docker-compose up mongodb


 | Successfully added user: {
 | 	"user" : "admin",
 | 	"roles" : [
 | 		{
 | 			"role" : "root",
 | 			"db" : "admin"
 | 		}
 | 	]
 | }


Another one version

MERN-Stack-Front-To-Back-v2.0


Connect MongoDB from Ubuntu

$ sudo apt install mongodb-clients


$ mongo "mongodb://localhost:27017/mongo-database"


https://stackoverflow.com/questions/34559557/how-to-enable-authentication-on-mongodb-through-docker


Execute from docker-compose

// OK!
$ docker-compose exec mongo mongo "mongodb://admin:password@localhost:27017/logs?authSource=admin&readPreference=primary&directConnection=true&ssl=false" --quiet --eval "db.logs.find().pretty()"


Alpine add telnet package

$ docker exec -it mern-stack-front-to-back-api sh

# apk add busybox-extras
# telnet mongodb 27017


IDE Compass

https://www.mongodb.com/try/download/compass


sudo dpkg -i mongodb-compass_1.36.4_amd64.deb