Docker两层应用程序问题:无法连接到mongo容器

时间:2017-07-25 15:34:17

标签: docker docker-compose

我有一个简单的nodeJS应用程序,包括前端和mongo数据库。我想通过Docker部署它。

在我的docker-compose文件中,我有以下内容:

version: '2'
services:
  express-container:
    build: .
    ports:
     - "3000:3000"
    depends_on:
     - mongo-container
  mongo-container:
    image: mongo:3.0

当我运行docker-compose时,我有以下错误:

Creating todoangularv2_mongo-container_1 ...
Creating todoangularv2_mongo-container_1 ... done
Creating todoangularv2_express-container_1 ...
Creating todoangularv2_express-container_1 ... done
Attaching to todoangularv2_mongo-container_1, todoangularv2_express-container_1
mongo-container_1    | 2017-07-25T15:26:09.863+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=25f03f51322b
mongo-container_1    | 2017-07-25T15:26:09.864+0000 I CONTROL  [initandlisten] db version v3.0.15
mongo-container_1    | 2017-07-25T15:26:09.864+0000 I CONTROL  [initandlisten] git version: b8ff507269c382bc100fc52f75f48d54cd42ec3b
mongo-container_1    | 2017-07-25T15:26:09.864+0000 I CONTROL  [initandlisten] build info: Linux ip-10-166-66-3 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
mongo-container_1    | 2017-07-25T15:26:09.864+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongo-container_1    | 2017-07-25T15:26:09.864+0000 I CONTROL  [initandlisten] options: {}
mongo-container_1    | 2017-07-25T15:26:09.923+0000 I JOURNAL  [initandlisten] journal dir=/data/db/journal
mongo-container_1    | 2017-07-25T15:26:09.924+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
express-container_1  | Listening on port 3000
express-container_1  |
express-container_1  | events.js:72
express-container_1  |         throw er; // Unhandled 'error' event
express-container_1  |               ^
express-container_1  | Error: failed to connect to [mongo-container:27017]

所以我的前端无法到达docker-compose文件中名为'mongo-container'的mongo容器。在应用程序本身中,我给出了mongo数据库的URL,如下所示:

module.exports = {
    url : 'mongodb://mongo-container:27017/todo'
}

知道如何更改我的应用程序,以便在Docker上运行时,我没有这个连接问题吗?

编辑:mongo容器提供以下输出:

WAUTERW-M-T3ZT:vagrant wim$ docker logs f63
2017-07-26T09:15:02.824+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=f637f963c87f
2017-07-26T09:15:02.825+0000 I CONTROL  [initandlisten] db version v3.0.15
2017-07-26T09:15:02.825+0000 I CONTROL  [initandlisten] git version: b8ff507269c382bc100fc52f75f48d54cd42ec3b
...
2017-07-26T09:15:21.461+0000 I STORAGE  [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB,  took 0.024 secs
2017-07-26T09:15:21.476+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

快递容器提供以下输出:

WAUTERW-M-T3ZT:vagrant wim$ docker logs 25a
Listening on port 3000

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [mongo-container:27017]
    at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:555:74)
    at EventEmitter.emit (events.js:106:17)
    at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
    at EventEmitter.emit (events.js:98:17)
    at Socket.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:441:14
    at process._tickCallback (node.js:415:13)

编辑:问题出现在Dockerfile中。这是一个更正的(我从节点图像而不是Ubuntu图像开始简化了一点):

FROM node:0.10.40
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install
CMD ["node", "/usr/src/app/bin/www"]

2 个答案:

答案 0 :(得分:0)

您可以在depends_on会话之前替换links,表示depends_on等服务之间的依赖关系,并且根据documentation,链接服务的容器可以在与别名相同的主机名,如果未指定别名,则为服务名称。

version: '2'
services:
  express-container:
    build: .
    ports:
     - "3000:3000"
    links:
     - "mongo-container"
  mongo-container:
    image: mongo:3.0

答案 1 :(得分:0)

问题出现在Dockerfile中。这是一个更正的(我从节点图像而不是Ubuntu图像开始简化了一点):

FROM node:0.10.40
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install
CMD ["node", "/usr/src/app/bin/www"]