我有基于loopback的restful API连接到mongodb。它工作正常。但是,当我将它放入码头集装箱时,它将无法连接。码头容器中的环回不能连接到mongo
DockerFile
# Create image based on the official Node 6 image from the dockerhub
FROM node:6
# Create a directory where our app will be placed
RUN mkdir -p /opt/src/app
# Change directory so that our commands run inside this new directory
WORKDIR /opt/src/app
# Copy dependency definitions
COPY package.json /opt/src/app
# Install dependecies
RUN npm install
# Get all the code needed to run the app
COPY . /opt/src/app
# Expose the port the app runs in
EXPOSE 3000
# Serve the app
CMD ["npm", "start"]
.dockerignore
node_modules/
的package.json
{
"name": "my-api",
"version": "1.0.0",
"main": "server/server.js",
"scripts": {
"lint": "eslint .",
"start": "node . -H 0.0.0.0",
"posttest": "npm run lint && nsp check",
"build:sdk": "./node_modules/.bin/lb-sdk server/server.js ../project-app/src/app/shared/sdk -l angular2 -d ng2web -i enabled"
},
"dependencies": {
"compression": "^1.0.3",
"cors": "^2.5.2",
"helmet": "^1.3.0",
"lodash": "^4.17.4",
"loopback": "3.1.1",
"loopback-boot": "^2.23.0",
"loopback-component-explorer": "^4.0.0",
"loopback-connector-mongodb": "^1.18.0",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^1.0.1"
},
"devDependencies": {
"@mean-expert/loopback-sdk-builder": "^2.1.0-rc.8.2",
"eslint": "^2.13.1",
"eslint-config-loopback": "^4.0.0",
"nsp": "^2.1.0"
},
"repository": {
"type": "",
"url": ""
},
"license": "UNLICENSED",
"description": "my-api"
}
构建成容器
$ docker build -t my-api:dev . --no-cache=true
运行它
$ docker run -it --name my-api -p 3000:3000 my-api:dev
错误
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info lifecycle [email protected]~prestart: [email protected]
npm info lifecycle [email protected]~start: [email protected]
> [email protected] start /opt/src/app
> node . -H 0.0.0.0
Web server listening at: http://127.0.0.1:3000
Browse your REST API at http://127.0.0.1:3000/explorer
Connection fails: MongoError: failed to connect to server [localhost:27017] on first connect
It will be retried for the next request.
/opt/src/app/node_modules/mongodb/lib/mongo_client.js:336
throw err
^
MongoError: failed to connect to server [localhost:27017] on first connect
at Pool.<anonymous> (/opt/src/app/node_modules/mongodb-core/lib/topologies/server.js:326:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (/opt/src/app/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (/opt/src/app/node_modules/mongodb-core/lib/connection/connection.js:175:49)
at Socket.g (events.js:291:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1278:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
我错过了什么,以及如何解决它?
谢谢你kisiel。我明白了。 – Shawn
如果我不想在每次部署时都更改docker的mongo连接代码,该怎么办?我在没有docker的情况下进行开发和测试,因此每次需要发布代码时都会产生一个更改ip的问题。 **任何建议?** –
@Shawn您是否设法解决此问题?我遇到同样的问题,但无法理解上述提供的解决方案。你能通过我的问题在这里[链接](https://stackoverflow.com/questions/47314547/docker-compose-with-loopback-and-mongodb-fail-to-connect-mongdb) – surajnew55