Docker MongoDB DataBase


$ 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

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


Alpine add telnet package

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

# apk add busybox-extras
# telnet mongodb 27017