我想制作简单的RESTful API。我正在使用docker来做到这一点。这是我的Dockerfile
:
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
root@67062897d4f0:/# 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 :
connect@src/mongo/shell/mongo.js:229:14
root@67062897d4f0:/# 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
有人可以帮我解决这个问题吗?
答案 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
答案 1 :(得分:1)
太棒了,我很高兴找到了这个帖子。我将为其他人添加一些关键字。
如果收到此错误消息,您尝试从Docker容器内部访问时,可能会在本地计算机上运行MongoDB。原因是在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]' }
可能的修复:
启用MongoDB的远程连接,并使用您的公共IP从Docker Container内部访问它。
从Container内部运行MongoDB,然后您可以使用localhost引用它。
运行另一个Docker Container,并在其上启用远程连接。调查MongoDB配置文件中的bind_ip
。确保使用auth凭据保护数据库。花一些时间确保它是安全的。
MongoDB中默认禁用远程连接,即:错误地通过Docker容器。
这是一个有用的资源:https://hub.docker.com/_/mongo/
答案 2 :(得分:0)
对于 docker-compose ,您需要使用标签networks
将一个容器节点与另一个容器节点链接。看:
version: '2'
networks:
app-tier:
driver: bridge
services:
mongodb:
image: 'mongodb:latest'
networks:
- app-net
myapp:
image: 'YOUR_APPLICATION_IMAGE'
networks:
- app-net