{"id":4399,"date":"2022-10-28T20:15:38","date_gmt":"2022-10-28T11:15:38","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=4399"},"modified":"2022-10-28T21:48:15","modified_gmt":"2022-10-28T12:48:15","slug":"4399","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=4399","title":{"rendered":"<small>Docker : mongoDB and mongo-express install<\/small>"},"content":{"rendered":"<p>1. Reference<br \/>\n<a href=\"https:\/\/laid-back-scientist.com\/docker-mongo\">https:\/\/laid-back-scientist.com\/docker-mongo<\/a><\/p>\n<p>2. Create directory<\/p>\n<pre>\r\n$ mkdir MongoDB\r\n$ cd MongoDB\r\n<\/pre>\n<p>3. docker-compose.yml ( volumes )<br \/>\n(1) Reference<br \/>\n<a href=\"https:\/\/docs.docker.jp\/compose\/compose-file\/compose-file-v3.html?highlight=volumes#volumes\">https:\/\/docs.docker.jp\/compose\/compose-file\/compose-file-v3.html?highlight=volumes#volumes<\/a><\/p>\n<p>(2) short syntax<\/p>\n<pre>\r\n    volumes:\r\n      - [source:]target[:mode]\r\n# source: \u30db\u30b9\u30c8\u4e0a\u306e\u30d1\u30b9\u3092\u6307\u5b9a\u3059\u308b\u6642\u306f\u3001Compose \u30d5\u30a1\u30a4\u30eb\u304b\u3089\u306e\u76f8\u5bfe\u30d1\u30b9\u3092\u6307\u5b9a\r\n      - .\/mongodb_data\r\n      - .\/configdb\r\n<\/pre>\n<p>(3) short syntax explanation<\/p>\n<pre>\r\n\u77ed\u3044\u66f8\u5f0f \u306e\u5f62\u5f0f\u306f\u3001 [\u30bd\u30fc\u30b9:]\u30bf\u30fc\u30b2\u30c3\u30c8[:\u30e2\u30fc\u30c9] \r\n\u30bd\u30fc\u30b9 \u306e\u5834\u6240\u306b\u306f\u3001\u30db\u30b9\u30c8\u4e0a\u306e\u30d1\u30b9\u307e\u305f\u306f\u30dc\u30ea\u30e5\u30fc\u30e0\u540d\u306e\u3069\u3061\u3089\u304b\u3092\u6307\u5b9a\r\n\u30bf\u30fc\u30b2\u30c3\u30c8 \u3068\u306f\u3001\u30dc\u30ea\u30e5\u30fc\u30e0\u304c\u30de\u30a6\u30f3\u30c8\u3055\u308c\u308b\u30b3\u30f3\u30c6\u30ca\u4e0a\u306e\u30d1\u30b9\r\n\u6a19\u6e96\u7684\u306a\u30e2\u30fc\u30c9\u3001 ro \u306f \u8aad\u307f\u8fbc\u307f\u5c02\u7528\u3001rw \u306f \u8aad\u307f\u66f8\u304d \uff08\u30c7\u30d5\u30a9\u30eb\u30c8\uff09\r\n<\/pre>\n<p>4. Create docker-compose.yml<\/p>\n<pre>\r\n$ vi docker-compose.yml\r\n$ cat docker-compose.yml\r\nversion: '3.9'\r\n\r\nservices:\r\n  mongo:\r\n    image: mongo\r\n    container_name: mongo_db\r\n    restart: always\r\n    environment:\r\n      MONGO_INITDB_ROOT_USERNAME: root\r\n      MONGO_INITDB_ROOT_PASSWORD: password\r\n    ports:\r\n      - 27017:27017\r\n    volumes:\r\n      - .\/mongodb_data:\/data\/db\r\n      - .\/configdb:\/data\/configdb\r\n\r\n  mongo-express:\r\n    image: mongo-express\r\n    container_name: mongo_express\r\n    restart: always\r\n    ports:\r\n      - 8081:8081\r\n    environment:\r\n      ME_CONFIG_MONGODB_ADMINUSERNAME: root\r\n      ME_CONFIG_MONGODB_ADMINPASSWORD: password\r\n      ME_CONFIG_MONGODB_SERVER: mongo\r\n    depends_on:\r\n      - mongo\r\n\r\n$\r\n<\/pre>\n<p>3. docker-compose up<\/p>\n<pre>\r\n$ docker-compose up -d\r\nPulling mongo-express (mongo-express:)...\r\nlatest: Pulling from library\/mongo-express\r\n6a428f9f83b0: Pull complete\r\nf2b1fb32259e: Pull complete\r\n40888f2a0a1f: Pull complete\r\n4e3cc9ce09be: Pull complete\r\neaa1898f3899: Pull complete\r\nab4078090382: Pull complete\r\nae780a42c79e: Pull complete\r\ne60224d64a04: Pull complete\r\nDigest: sha256:2a25aafdf23296823b06bc9a0a2af2656971262041b8dbf11b40444804fdc104\r\nStatus: Downloaded newer image for mongo-express:latest\r\nCreating mongo_db ... done\r\nCreating mongo_express ... done\r\n$\r\n<\/pre>\n<p>4. Confirm ps<\/p>\n<pre>\r\n$ docker ps\r\nCONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                           NAMES\r\nec7777ff85ed   mongo-express   \"tini -- \/docker-ent\u2026\"   29 seconds ago   Up 20 seconds   0.0.0.0:8081->8081\/tcp, :::8081->8081\/tcp       mongo_express\r\n287b38a175b3   mongo           \"docker-entrypoint.s\u2026\"   31 seconds ago   Up 29 seconds   0.0.0.0:27017->27017\/tcp, :::27017->27017\/tcp   mongo_db\r\n\r\n$ \r\n<\/pre>\n<p>4. Confirm created directory<\/p>\n<pre>\r\n$ ls\r\nconfigdb  docker-compose.yaml  mongodb_data \r\n<\/pre>\n<p>5. Install pymongo<\/p>\n<pre>\r\n$ pip install pymongo\r\n<\/pre>\n<p>6. Create sample code<\/p>\n<pre>\r\n$ vi sample.py\r\n$ cat sample.py\r\nfrom pymongo import MongoClient\r\n\r\n\r\nHOST = 'localhost'\r\nPORT = 27017\r\nUSERNAME = 'root'\r\nPASSWORD = 'password'\r\n\r\nDB_NAME = 'demo_db'\r\nCOLLECTION_NAME = 'demo_collection'\r\n\r\n\r\nif __name__ == '__main__':\r\n    client = MongoClient(host=HOST, port=PORT, username=USERNAME, password=PASSWORD)\r\n    db = client[DB_NAME]\r\n    collection = db[COLLECTION_NAME]\r\n\r\n    data = [\r\n        {'username': 'Alice', 'score': 100},\r\n        {'username': 'Bob', 'score': 90}\r\n    ]\r\n    collection.insert_many(data)\r\n<\/pre>\n<p>7. Execute sample.py<\/p>\n<pre>\r\n$ python3 sample.py \r\n<\/pre>\n<p>8. Confirm from browser<br \/>\n&#8211; http:\/\/localhost:8081<br \/>\n<img decoding=\"async\" loading=\"lazy\" width=\"807\" height=\"632\" src=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/10\/20221028-1.png\" alt=\"\" class=\"alignnone size-medium wp-image-4403\" srcset=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/10\/20221028-1.png 807w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/10\/20221028-1-300x235.png 300w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/10\/20221028-1-768x601.png 768w\" sizes=\"(max-width: 807px) 100vw, 807px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Reference https:\/\/laid-back-scientist.com\/docker-mongo 2. Create directory $ mkdir MongoDB $ cd MongoDB 3. &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/4399"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4399"}],"version-history":[{"count":19,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/4399\/revisions"}],"predecessor-version":[{"id":4401,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/4399\/revisions\/4401"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}