2016-11-13 139 views
1

的Dockerfile我的应用如下泊坞窗集装箱未能运行

   # Tells the Docker which base image to start. 
       FROM node 

       # Adds files from the host file system into the Docker container. 
       ADD . /app 

       # Sets the current working directory for subsequent instructions 
       WORKDIR /app 

       RUN npm install 
       RUN npm install -g bower 
       RUN bower install --allow-root 
       RUN npm install -g nodemon 

       #expose a port to allow external access 
       EXPOSE 9000 9030 35729 

       # Start mean application 
       CMD ["nodemon", "server.js"] 

泊坞窗,compose.yml文件如下

      web: 
           build: . 
           links: 
              - db 
            ports: 
              - "9000:9000" 
              - "9030:9030" 
              - "35729:35729" 
            db: 
              image: mongo:latest 
              ports: 
                - "27017:27017" 

而产生的,同时运行如下的错误: -

web_1 | [nodemon] 1.11.0 
    web_1 | [nodemon] to restart at any time, enter `rs` 
    web_1 | [nodemon] watching: *.* 
    web_1 | [nodemon] starting `node server.js` 
    web_1 | Server running at http://127.0.0.1:9000 
    web_1 | Server running at https://127.0.0.1:9030 
    web_1 | 
    web_1 | /app/node_modules/mongodb/lib/server.js:261 
    web_1 |   process.nextTick(function() { throw err; }) 
    web_1 |          ^
    web_1 | MongoError: failed to connect to server [localhost:27017] on first connect 
    web_1 |  at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:313:35) 
    web_1 |  at emitOne (events.js:96:13) 
    web_1 |  at Pool.emit (events.js:188:7) 
    web_1 |  at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:271:12) 
web_1 |  at Connection.g (events.js:291:16) 
web_1 |  at emitTwo (events.js:106:13) 
web_1 |  at Connection.emit (events.js:191:7) 
web_1 |  at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:165:49) 
web_1 |  at Socket.g (events.js:291:16) 
web_1 |  at emitOne (events.js:96:13) 
web_1 |  at Socket.emit (events.js:188:7) 
web_1 |  at emitErrorNT (net.js:1281:8) 
web_1 |  at _combinedTickCallback (internal/process/next_tick.js:74:11) 
web_1 |  at process._tickCallback (internal/process/next_tick.js:98:9) 
web_1 | [nodemon] app crashed - waiting for file changes before starting... 

我已经上传的图片对我在DockerHub应用程序crissi/airlineInsurance。

回答

0

在docker中,您无法通过本地主机连接到其他容器,因为每个容器都是独立的,并且拥有自己的IP。你应该使用container_name:port。在你的例子中,它应该是db:27017从'web'中的NodeJS应用程序连接到'db'中的MongoDB。

所以这不是你的Dockerfile的问题。这是来自NodeJS应用程序的指向localhost而不是db的连接URL。

+0

我已经更改了我的应用程序配置文件。上面指定的数据库问题已解决。但是应用程序不会在端口9000,9030上加载。 – cmr

+0

我想你正在使用Boot2Docker。 Boot2Docker有一个自己的IP地址,因为它是一个虚拟机。您可以使用像[这里](https://stackoverflow.com/questions/28403341/boot2docker-access-webserver-as-localhost#28405278)的端口转发,或者您可以通过docker inspect $获得Boot2Docker IP(docker ps - q)| grep IPA'并使用它像Boot2DockerIP:端口,因为端口被转发到Boot2Docker。 – Piu130