2017-03-23 227 views
1

我想製作簡單的restful API。我正在使用docker來做到這一點。這裏是我的Dockerfiledocker嘗試連接到mongodb時出錯

FROM mongo:3.2 
EXPOSE 3000 
RUN apt-get update; apt-get install curl -y 
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - 
RUN apt-get install -y nodejs 
ADD . . 
CMD node app.js 

docker-compose.yml看起來是這樣的:

version: '2' 
services: 
    db: 
    build: ../images/mongodb 
    ports: 
    - "27017:27017" 
    - "3000:3000" 

app.js文件看起來像這樣:

var express = require('express') 
var app = express() 
var mongoose = require('mongoose'); 
mongoose.Promise = global.Promise; 
mongoose.connect('mongodb://127.0.0.1:27017/sample'); 
app.get('/', function (req, res) { 
    res.send('Hello World!') 
}) 


app.listen(3000, function() { 
    console.log('Example app listening on port 3000!') 
}) 

在運行時docker-compose up --build,我有錯:

db_1  | Example app listening on port 3000! 
db_1  | 
db_1  | events.js:160 
db_1  |  throw er; // Unhandled 'error' event 
db_1  |  ^
db_1  | MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] 
db_1  |  at Pool.<anonymous> (/node_modules/mongodb-core/lib/topologies/server.js:327:35) 
db_1  |  at emitOne (events.js:96:13) 
db_1  |  at Pool.emit (events.js:188:7) 
db_1  |  at Connection.<anonymous> (/node_modules/mongodb-core/lib/connection/pool.js:274:12) 
db_1  |  at Connection.g (events.js:291:16) 
db_1  |  at emitTwo (events.js:106:13) 
db_1  |  at Connection.emit (events.js:191:7) 
db_1  |  at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:177:49) 
db_1  |  at Socket.g (events.js:291:16) 
db_1  |  at emitOne (events.js:96:13) 
db_1  |  at Socket.emit (events.js:188:7) 
db_1  |  at emitErrorNT (net.js:1281:8) 
db_1  |  at _combinedTickCallback (internal/process/next_tick.js:80:11) 
db_1  |  at process._tickCallback (internal/process/next_tick.js:104:9) 

我曾嘗試在泊塢窗機一起去docker run -it mongo:3.2 /bin/bash

[email protected]:/# mongo 
MongoDB shell version: 3.2.12 
connecting to: test 
2017-03-23T07:01:07.587+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused 
2017-03-23T07:01:07.588+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : 
[email protected]/mongo/shell/mongo.js:229:14 

[email protected]:/# mongod 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] MongoDB starting : pid=29 port=27017 dbpath=/data/db 64-bit host=67062897d4f0 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] db version v3.2.12 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] git version: ef3e1bc78e997f0d9f22f45aeb1d8e3b6ac14a14 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] allocator: tcmalloc 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] modules: none 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] build environment: 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten]  distmod: debian81 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten]  distarch: x86_64 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten]  target_arch: x86_64 
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] options: {} 
2017-03-23T07:01:12.029+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=8G,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), 
2017-03-23T07:01:12.222+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 
2017-03-23T07:01:12.222+0000 I CONTROL [initandlisten] 
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten] 
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten] **  We suggest setting it to 'never' 
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten] 
2017-03-23T07:01:12.228+0000 I FTDC  [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data' 
2017-03-23T07:01:12.228+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker 
2017-03-23T07:01:12.283+0000 I NETWORK [initandlisten] waiting for connections on port 27017 
^C2017-03-23T07:01:13.447+0000 I CONTROL [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends 
2017-03-23T07:01:13.448+0000 I FTDC  [signalProcessingThread] Shutting down full-time diagnostic data capture 
2017-03-23T07:01:13.453+0000 I CONTROL [signalProcessingThread] now exiting 
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets... 
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] closing listening socket: 5 
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] closing listening socket: 6 
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock 
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog... 
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] shutdown: going to close sockets... 
2017-03-23T07:01:13.453+0000 I STORAGE [signalProcessingThread] WiredTigerKVEngine shutting down 
2017-03-23T07:01:13.586+0000 I STORAGE [signalProcessingThread] shutdown: removing fs lock... 
2017-03-23T07:01:13.586+0000 I CONTROL [signalProcessingThread] dbexit: rc: 0 

有人可以幫我解決這個問題?

回答

2

我認爲這個問題是使用CMD node app.js只啓動節點而不是數據庫。通常在創建這種簡單的應用程序時,您使用兩個容器。一個用於節點的mongo,因爲您不使用mongo或節點的自定義配置,您只需使用映像並將當前文件夾中的代碼映射到容器中的/ opt /目錄,這意味着您所需要的只是一個撰寫文件,可能不需要任何Dockerfile的

免責聲明顯然這不是生產足夠好,在某些時候,你可能會想使用一個Dockerfile爲您的應用程序,因爲它是最好的開始節點作爲用戶而不是root,並將這些文件放在一個有意義的目錄中。但是,如果你想要做的就是作爲沙箱使用,這應該做的。

你搬運工撰寫會是什麼樣子

version: "2" 

services: 
    db: 
    image: mongo:3.2 
    ports: 
     - 27017 
    app: 
    image: node 
    links: 
     - db 
    volumes: 
     - '.:/opt/' 
    command: node /opt/app.js 
    ports: 
     - 3000:3000 

如果你想要去的充滿並指定Dockerfile每個。

/泊塢窗,文件/程序/ Dockerfile

# take from the latest node build 
FROM node 

# Make a directory /opt/app 
RUN mkdir /opt/app 

# Set work dir to /opt/app 
WORKDIR /opt/app 

# Do all your npm install etc.... 

CMD node app.js 

/泊塢窗,文件/數據庫/ Dockerfile

FROM mongo:3.2 

# Do some fancy mongo stuff. 

/docker-compose.yml

version: "2" 

services: 
    db: 
    build: 
     context: docker-files/db 
     dockerfile: Dockerfile 
    ports: 
     - 27017 
    app: 
    build: 
     context: docker-files/app 
     dockerfile: Dockerfile 
    links: 
     - db 
    volumes: 
     - '.:/opt/app' 
    ports: 
     - 3000:3000 
+0

感謝您的回答,如何設置路徑爲我Dockerfiles數據庫和應用程序 – dmx

+0

改變了我的答案@dmx讓我知道,如果這是你的意思 –

1

最好的,我很高興我找到了這個線索。我將爲其他人添加一些關鍵字。

如果您收到此錯誤消息,您可能在本地計算機上運行MongoDB,同時嘗試從Docker容器中訪問它。原因是在Docker容器中,localhost或127.0.0.1引用的是不同於真實本地機器的接口。

錯誤輸出:

MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] 
    at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:329:35) 
    at emitOne (events.js:96:13) 
    at Pool.emit (events.js:191:7) 
    at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12) 
    at Object.onceWrapper (events.js:293:19) 
    at emitTwo (events.js:106:13) 
    at Connection.emit (events.js:194:7) 
    at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:187:49) 
    at Object.onceWrapper (events.js:293:19) 
    at emitOne (events.js:96:13) 
    at Socket.emit (events.js:191:7) 
    at emitErrorNT (net.js:1284:8) 
    at _combinedTickCallback (internal/process/next_tick.js:80:11) 
    at process._tickDomainCallback (internal/process/next_tick.js:128:9) 
name: 'MongoError', 
message: 'failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]' } 

Possible Fixes:

  1. 從MongoDB中啓用遠程連接,並使用你的公網IP從泊塢窗Container中訪問它。

  2. 從容器中運行MongoDB,然後你可以用localhost引用它。

  3. 運行另一個Docker容器,並在其上啓用遠程連接。調查MongoDB配置文件中的bind_ip。確保您使用身份驗證憑據來保護您的數據庫。花一些時間確保它是安全的。

Remote connections are disabled by default in MongoDB, ie: going through a Docker container incorrectly.

這裏是一個有用的資源:https://hub.docker.com/_/mongo/

相關問題