简体   繁体   English

docker容器中的环回无法连接到mongo

[英]loopback in docker container cannot connect to mongo

I have loopback based restful api that connects to mongodb. 我有连接到mongodb的基于环回的restful api。 It works fine. 工作正常。 But when I put it into a docker container it won't connect. 但是,当我将其放入docker容器时,它将无法连接。

DockerFile Docker文件

# 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 .dockerignore

node_modules/

package.json 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"
}

Build into container 内置到容器中

$ docker build -t my-api:dev . --no-cache=true

Run it 运行

$ docker run -it --name my-api -p 3000:3000 my-api:dev

Error 错误

npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.10.0
npm info lifecycle my-api@1.0.0~prestart: my-api@1.0.0
npm info lifecycle my-api@1.0.0~start: my-api@1.0.0

> my-api@1.0.0 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)

What did I miss and how to fix it? 我错过了什么以及如何解决?

Check what is Your ip in docker network (and make sure MongoDB is listening on this ip) ifconfig Should give You sth like: 检查docker网络中的IP是什么(并确保MongoDB正在监听该IP) ifconfig应该给您以下信息:

docker0: flags=4099 mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 docker0:标志= 4099 mtu 1500 inet 172.17.0.1网络掩码255.255.0.0广播0.0.0.0

I've bolded the IP You should use in Your node app. 我已经加粗了您应该在您的节点应用程序中使用的IP。

If you are running MongoDB from a Dockerized container, then just replace your "hostname" with MongoDB's "container" name. 如果您是从Dockerized容器运行MongoDB,则只需将您的“主机名”替换为MongoDB的“容器”名称。

$ docker ps $ docker ps

CONTAINER ID     IMAGE    COMMAND                  CREATED             STATUS              PORTS                            NAMES
baf89457ec65     mongo    "docker-entrypoint..."   8 minutes ago       Up 9 minutes        0.0.0.0:32779->27017/tcp         mongodb

In Loopback's "datasources.json", the correct entry should be: 在Loopback的“ datasources.json”中,正确的条目应为:

"mongo": { "host": "mongodb", "port": 27017, ... “ mongo”:{“ host”:“ mongodb”,“ port”:27017,...

You can setup Docker with Node.js and MongoDB running together using "docker-compose". 您可以使用“ docker-compose”将Docker与Node.js和MongoDB一起运行。 Follow instructions here . 请按照此处的说明进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM