@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Docker で起動した MongoDB に接続する

ポイントは、 mongod コンテナ(サーバ)と mongo コンテナ(クライアント)を同じ network に所属させること。

ネットワーク作成

❯ docker network create some-network
e8d64e37962861363dede64b437a7d58359f9db1eb57d8b27f5f73f92fdfbf5d

mongod 起動

❯ docker run --rm --name some-mongo --network some-network -d mongo:latest
de452a39c4f13f44dce88b0ab41c2a73af7e189dee69400b9cfc62f169c880bf

mongo シェルを起動して mongod に接続

❯ docker run -it --rm --network some-network mongo:latest mongo --host some-mongo test
MongoDB shell version v4.4.3
connecting to: mongodb://some-mongo:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a0020bc3-1590-4dd4-8366-75adb966352d") }
MongoDB server version: 4.4.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
    https://community.mongodb.com
---
The server generated these startup warnings when booting:
        2021-01-26T08:10:47.064+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-01-26T08:10:47.239+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>