Docker : mongoDB and mongo-express install

1. Reference
https://laid-back-scientist.com/docker-mongo

2. Create directory

$ mkdir MongoDB
$ cd MongoDB

3. docker-compose.yml ( volumes )
(1) Reference
https://docs.docker.jp/compose/compose-file/compose-file-v3.html?highlight=volumes#volumes

(2) short syntax

    volumes:
      - [source:]target[:mode]
# source: ホスト上のパスを指定する時は、Compose ファイルからの相対パスを指定
      - ./mongodb_data
      - ./configdb

(3) short syntax explanation

短い書式 の形式は、 [ソース:]ターゲット[:モード] 
ソース の場所には、ホスト上のパスまたはボリューム名のどちらかを指定
ターゲット とは、ボリュームがマウントされるコンテナ上のパス
標準的なモード、 ro は 読み込み専用、rw は 読み書き (デフォルト)

4. Create docker-compose.yml

$ vi docker-compose.yml
$ cat docker-compose.yml
version: '3.9'

services:
  mongo:
    image: mongo
    container_name: mongo_db
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - 27017:27017
    volumes:
      - ./mongodb_data:/data/db
      - ./configdb:/data/configdb

  mongo-express:
    image: mongo-express
    container_name: mongo_express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
      ME_CONFIG_MONGODB_SERVER: mongo
    depends_on:
      - mongo

$

3. docker-compose up

$ docker-compose up -d
Pulling mongo-express (mongo-express:)...
latest: Pulling from library/mongo-express
6a428f9f83b0: Pull complete
f2b1fb32259e: Pull complete
40888f2a0a1f: Pull complete
4e3cc9ce09be: Pull complete
eaa1898f3899: Pull complete
ab4078090382: Pull complete
ae780a42c79e: Pull complete
e60224d64a04: Pull complete
Digest: sha256:2a25aafdf23296823b06bc9a0a2af2656971262041b8dbf11b40444804fdc104
Status: Downloaded newer image for mongo-express:latest
Creating mongo_db ... done
Creating mongo_express ... done
$

4. Confirm ps

$ docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                           NAMES
ec7777ff85ed   mongo-express   "tini -- /docker-ent…"   29 seconds ago   Up 20 seconds   0.0.0.0:8081->8081/tcp, :::8081->8081/tcp       mongo_express
287b38a175b3   mongo           "docker-entrypoint.s…"   31 seconds ago   Up 29 seconds   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   mongo_db

$ 

4. Confirm created directory

$ ls
configdb  docker-compose.yaml  mongodb_data 

5. Install pymongo

$ pip install pymongo

6. Create sample code

$ vi sample.py
$ cat sample.py
from pymongo import MongoClient


HOST = 'localhost'
PORT = 27017
USERNAME = 'root'
PASSWORD = 'password'

DB_NAME = 'demo_db'
COLLECTION_NAME = 'demo_collection'


if __name__ == '__main__':
    client = MongoClient(host=HOST, port=PORT, username=USERNAME, password=PASSWORD)
    db = client[DB_NAME]
    collection = db[COLLECTION_NAME]

    data = [
        {'username': 'Alice', 'score': 100},
        {'username': 'Bob', 'score': 90}
    ]
    collection.insert_many(data)

7. Execute sample.py

$ python3 sample.py 

8. Confirm from browser
– http://localhost:8081