I don't want to download the database to a local environment such as Mac when developing an app, so I use a container management tool called Docker. Because the transition is easy. I want to use it in the production environment in the mountains, but I don't have any knowledge about Kubernetes, so I can't really step into it. Well, it depends on the scale.
This article is like a sample of building ** MongoDB ** using ** Docker **. There was nothing else that was easy to understand, and I remember checking it when I made it.
The operating environment is Mac OS Catalina
Goal Create a ** docker-compose.yml ** file and put ** MongoDB ** and ** mongo-express ** on localhost.
** MongoDB ** is NoSQL ** mongo-express ** is like ** phpMyAdmin ** in MySQL. The contents of ** MongoDB ** are easier to see.
docker-compose.yml
# Use root/example as user/password credentials
version: "3.1"
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root #username
MONGO_INITDB_ROOT_PASSWORD: tree #password
ports:
- 27014:27017
volumes:
- ./db:/data/db
- ./configdb:/data/configdb
mongo-express:
image: mongo-express
restart: always
ports:
- 8080:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root #username
ME_CONFIG_MONGODB_ADMINPASSWORD: tree #password
See below for the basic way to write a ** docker-compose.yml ** file. https://docs.docker.com/compose/compose-file/
See below for ** ports ** specifications https://qiita.com/tksugimoto/items/23fcce1b067661e8aa46#%E6%A4%9C%E8%A8%BC%E7%B5%90%E6%9E%9C
The database file will be created in the location specified by ** volumes **. If there are files in ** sample / docker-compose.yml **, ** sample / db ** and ** sample / configdb ** will be created.
How To Use
whoami$ pwd
/Users/whoami/sample
whoami$ ls
docker-compose.yml
whoami$ docker-compose up -d // -d means run as daemon
Creating network "sample_default" with the default driver
Creating sample_mongo-express_1 ... done
Creating sample_mongo_1 ... done
whoami$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1aeb90deadd0 mongo "docker-entrypoint.s…" 23 seconds ago Up 22 seconds 0.0.0.0:27014->27017/tcp sample_mongo_1
6649bb697412 mongo-express "tini -- /docker-ent…" 23 seconds ago Up 9 seconds 0.0.0.0:8080->8081/tcp sample_mongo-express_1
whoami$ docker-compose down
Stopping sample_mongo_1 ... done
Stopping sample_mongo-express_1 ... done
Removing sample_mongo_1 ... done
Removing sample_mongo-express_1 ... done
Removing network sample_default
whoami$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
After ** docker-compose up -d **, open [http: // localhost: 8080 /] in your browser and you will find ** mongo-express ** there.
** MongoDB ** will not work if you specify the port number too much. I think I've seen an article that says Docker's MongoDB port is only 27017. It seems that mongo-express can only be used in the development environment with docker.
I will also write a method to connect to the newly created MongoDB from Node.js.
https://hub.docker.com/_/mongo
https://hub.docker.com/_/mongo-express