run MongoDB in Docker container

PS C:\Users\【user]> docker pull mongo:latest
latest: Pulling from library/mongo
10ac4908093d: Pull complete
685504455d09: Pull complete
ebd36404f329: Pull complete
3abd9b25affb: Pull complete
2d7fde532eae: Pull complete
24fc70e4c7d7: Pull complete
ffc2353072f7: Pull complete
560de8e3a6c7: Pull complete
0748cd1d792c: Pull complete
Digest: sha256:2374c2525c598566cc4e62145ba65aecfe1bd3bf090cccce1ca44f3e2b60f861
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

  

PS C:\Users\[user]> docker run -d --name mongo_local_Test -p 27020:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=paSsw0rd mongo
840a5b0dd13fa7df72d69d59ec9b43f81841d10230aec8755b043f6bd007c672

  

=>mongodb://admin:paSsw0rd@localhost:27020/?authMechanism=DEFAULT

 

PS C:\Users\[user]> docker exec -ti mongo_local_Test bash
root@840a5b0dd13f:/# mongo
bash: mongo: command not found
root@840a5b0dd13f:/# mongosh
Current Mongosh Log ID: 63f8327d328712821d9fa1e4
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB:          6.0.4
Using Mongosh:          1.6.2

  

root@840a5b0dd13f:/# mongosh -u admin -p paSsw0rd
Current Mongosh Log ID: 63f832e1ec1b406b397debe6
Connecting to:          mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB:          6.0.4
Using Mongosh:          1.6.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-02-24T03:37:33.203+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2023-02-24T03:37:34.037+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2023-02-24T03:37:34.037+00:00: vm.max_map_count is too low
------

------
   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()
------

test> show dbs
admin   100.00 KiB
config   12.00 KiB
local    72.00 KiB
test> use test
already on db test
test> show dbs
admin   100.00 KiB
config   60.00 KiB
local    72.00 KiB

  

test> db.testC.insert({"_id":1})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{ acknowledged: true, insertedIds: { '0': 1 } }
test> show collections
testC
test> db.testC
test.testC
test> db.testC.find()
[ { _id: 1 } ]
test>

  

 

mongoDB 在docker中运行成功, 如何 将本地应用的数据 存储到docker的 mongodb中?

# test
docker pull mongo:latest

docker run -d --name mongo_local_Test -p 27020:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=paSsw0rd mongo

mongodb://admin:paSsw0rd@localhost:27020

mongosh --username admin --password paSsw0rd


docker network inspect bridge
 #find Gateway
docker run --detach -p 32770:21568 -e TJ_MONGODB_URI="mongodb://admin:paSsw0rd@localhost:27020"****backend-webapi:latest

  

 

 

方式二:

docker pull mongo:latest

  

 

mongdb_docker_compose.yaml

services:
  mongo-docker-local:
    image: mongo:latest
    container_name: mongo-docker-local
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: paSsw0rd
    ports:
      - 27023:27017
    networks:
      - server-net
    volumes:
      - mongodb_data_container:/data/db

volumes:
  mongodb_data_container:


networks:
  server-net:
    driver: bridge

  

docker compose -f .\backend_docker_compose.yaml up  

  

 

 

 

 

mongodb://admin:paSsw0rd@localhost:27023

 

 

本地访问 docker 中的mongo-db:mongodb://admin:paSsw0rd@localhost:27023

 

 

docker 访问 本地中的mongodb:mongodb://host.docker.internal:27017

 

 

root@2c23541aad84:/# mongosh "mongodb://host.docker.internal:27017"
Current Mongosh Log ID: 63f8781dbd21777f847bd9e3
Connecting to:          mongodb://host.docker.internal:27017/?directConnection=true&appName=mongosh+1.6.2
Using MongoDB:          6.0.0-rc4
Using Mongosh:          1.6.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-02-24T09:25:26.751+08: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()
------

test> show dbs
admin                                   40.00 KiB
config                                 108.00 KiB
local                                   96.00 KiB
sample_airbnb                           52.15 MiB
sample_analytics                         9.43 MiB
sample_geospatial                        1.02 MiB
sample_guides                           40.00 KiB
sample_mflix                            45.41 MiB
sample_restaurants                       5.97 MiB
sample_supplies                        968.00 KiB
sample_training                         41.48 MiB
sample_weatherdata                       2.38 MiB
test> exit
root@2c23541aad84:/# mongosh "mongodb://localhost:27017" -u admin -p paSsw0rd
Current Mongosh Log ID: 63f8782d749f0979f1b839da
Connecting to:          mongodb://<credentials>@localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB:          6.0.4
Using Mongosh:          1.6.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-02-24T08:33:40.388+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2023-02-24T08:33:41.227+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2023-02-24T08:33:41.227+00:00: vm.max_map_count is too low
------

------
   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()
------

test> show dbs
admin      100.00 KiB
config      60.00 KiB
cucpInput   16.00 KiB
local       72.00 KiB
test>

  

 

posted @ 2023-02-24 13:39  PanPan003  阅读(72)  评论(0编辑  收藏  举报