简体   繁体   English

Docker + MongoDB:如何使用RoboMongo查看数据库?

[英]Docker + MongoDB: How to view database with RoboMongo?

How can I access the mongo database that created by Docker using RoboMongo or something similar? 如何使用RoboMongo或类似方法访问Docker创建的mongo数据库?

Before using Doncker, I have already install mongodb into my Linux machine. 在使用Doncker之前,我已经将mongodb安装到了我的Linux机器中。 So I can't use the port "27017:27017" in the docker-compse.yml. 所以我不能在docker-compse.yml中使用端口“ 27017:27017”。 I use "27018:27018": 我使用“ 27018:27018”:

version: "2"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    links:
      - mongo
  mongo:
    image: mongo
    volumes:
      - /data/mongodb/db:/data/db
    ports:
      - "27018:27018"

I can use RoboMongo to view all the databases that I created in my pre-docker mongo. 我可以使用RoboMongo来查看我在docker之前的mongo中创建的所有数据库。 But how can I access the Docker's? 但是我如何访问Docker的呢?

This is my DockerFile: 这是我的DockerFile:

FROM mhart/alpine-node:latest
ADD package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/

WORKDIR /opt/app
ADD . /opt/app

EXPOSE 3000

CMD ["npm", "start"]

EDIT: 编辑:

When I run the docker on my terminal: 当我在终端上运行docker时:

$ docker-compose up
Starting dummyapp_mongo_1
Recreating dummyapp_web_1
Attaching to dummyapp_mongo_1, dummyapp_web_1
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=571498710dc0
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] db version v3.4.1
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] modules: none
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] build environment:
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten]     distmod: debian81
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongo_1  | 2017-01-03T10:20:35.083+0000 I CONTROL  [initandlisten] options: {}
mongo_1  | 2017-01-03T10:20:35.104+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongo_1  | 2017-01-03T10:20:35.104+0000 I STORAGE  [initandlisten] 
mongo_1  | 2017-01-03T10:20:35.104+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongo_1  | 2017-01-03T10:20:35.104+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongo_1  | 2017-01-03T10:20:35.104+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1454M,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] 
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] 
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] 
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] 
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
mongo_1  | 2017-01-03T10:20:36.385+0000 I CONTROL  [initandlisten] 
mongo_1  | 2017-01-03T10:20:36.389+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo_1  | 2017-01-03T10:20:36.390+0000 I NETWORK  [thread1] waiting for connections on port 27017
web_1    | 
web_1    | > dummy-app@0.0.0 start /opt/app
web_1    | > node ./bin/www

EDIT 2: 编辑2:

A new error below after changing the port to "27018:27017" when I am on GET http://localhost:3000/data/from/db or POST http://localhost:3000/data/into/db : 当我在GET http:// localhost:3000 / data / from / db或POST http:// localhost:3000 / data / into / db上时,将端口更改为“ 27018:27017”后,出现以下新错误:

failed to connect to server [mongo:27017] on first connect

MongoError: failed to connect to server [mongo:27017] on first connect
    at Pool.<anonymous> (/opt/app/node_modules/mongodb-core/lib/topologies/server.js:325:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (/opt/app/node_modules/mongodb-core/lib/connection/pool.js:270:12)
    at Connection.g (events.js:292:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (/opt/app/node_modules/mongodb-core/lib/connection/connection.js:173:49)
    at Socket.g (events.js:292:16)
    at emitOne (events.js:96:13)

Any ideas why? 有什么想法吗?

Are you sure you have your mongodb listening on port 27018 in the container? 您确定您的mongodb监听容器中的端口27018吗? Based solely on the information in your post, I'm guessing mongodb is listening on its default port, 27017, within the container, and you're mapping host 27018 to container 27018 which mongo isn't listening on. 仅基于您帖子中的信息,我猜测mongodb在容器中监听其默认端口27017,并且您正在将主机27018映射到mongo不在监听的容器27018。 That ports item should look like this: 27018:27017 so that the unused port on the host, 27018, is mapped to the correct port on the container, 27017. ports项应类似于: 27018:27017以便主机上未使用的端口27018映射到容器上的正确端口27017。

This exposure is irrelevant to the npm container because it uses a different mechanism to expose the ports, with a link between the containers. 此暴露与npm容器无关,因为它使用不同的机制来暴露端口,并在容器之间建立链接。 You'd find that if you put your other mongodb clients in containers, a link would also work for them. 您会发现,如果将其他mongodb客户端放入容器中,则链接也将对其起作用。 But to interact from the host, you'll need to expose the correct port in the container to an available port on the host. 但是要与主机进行交互,您需要将容器中的正确端口公开给主机上的可用端口。

https://docs.docker.com/compose/compose-file/#/ports https://docs.docker.com/compose/compose-file/#/ports

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM