2016-11-13 180 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

相關問題