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

时间:2017-01-03 11:02:49

标签: mongodb docker docker-compose dockerfile

如何使用RoboMongo或类似的东西访问Docker创建的mongo数据库?

在使用Doncker之前,我已经将mongodb安装到我的Linux机器上了。所以我不能使用端口" 27017:27017"在docker-compse.yml中。我使用" 27018:27018":

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

我可以使用RoboMongo查看我在pre-docker mongo中创建的所有数据库。但是我怎样才能访问Docker'

这是我的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"]

修改

当我在终端上运行泊坞窗时:

$ 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

编辑2:

将端口更改为" 27018:27017"后面的新错误当我在GET http://localhost:3000/data/from/db或POST http://localhost:3000/data/into/db时:

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)

任何想法为什么?

1 个答案:

答案 0 :(得分:0)

你确定你的mongodb正在收听容器中的端口27018吗?仅根据您帖子中的信息,我猜测mongodb正在监听容器内的默认端口27017,并且您将主机27018映射到容器27018,mongo不会监听。该ports项应如下所示:27018:27017以便主机上未使用的端口27018映射到容器27017上的正确端口。

此暴露与npm容器无关,因为它使用不同的机制来暴露端口,并在容器之间建立链接。您会发现,如果您将其他mongodb客户端放在容器中,则链接也适用于它们。但是要从主机进行交互,您需要将容器中的正确端口公开给主机上的可用端口。

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