[英]docker-compose connection between node and mongo
I read a lots of examples about connecting apps with docker, it seems really simple 我读了很多有关将应用程序与Docker连接的示例,这看起来真的很简单
Im my case I have 我的情况是
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
service:
build:
context: .
container_name: service
restart: always
ports:
- 3012:3012
depends_on:
- mongodb
links:
- mongodb
My connection in node is 我在节点中的连接是
const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://admin:admin@mongodb:27017/admin?replicaSet=rs0&slaveOk=true'
MongoClient.connect(mongoUrl, { useNewUrlParser: true }, (err, client) => {
console.log(err)
});
I have the following error 我有以下错误
{ MongoNetworkError: failed to connect to server [9d574801e4b4:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND 9d574801e4b4 9d574801e4b4:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
I do not understand why the host become 9d574801e4b4
我不明白为什么主机变成9d574801e4b4
When I run ping mongodb
into my container everything is fine 当我在容器中运行ping mongodb
一切都很好
PING mongodb (172.21.0.3) 56(84) bytes of data.
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=3 ttl=64 time=0.102 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=4 ttl=64 time=0.216 ms
^C
--- mongodb ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3156ms
rtt min/avg/max/mdev = 0.088/0.126/0.216/0.053 ms
By default mongo listens on localhost only. 默认情况下,mongo仅在localhost上侦听。 If it is for local development, simply bind it to all interfaces : 如果是用于本地开发,只需将其绑定到所有接口 :
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
command: --bind_ip_all
Just a guess here, but the depends_on
field in Docker Compose only means "this container has to wait until the other one is started." 这里只是一个猜测,但是Docker Compose中的depends_on
字段仅表示“此容器必须等待另一个容器启动”。 The service inside doesn't necessarily have to be up and running yet. 内部的服务不一定必须启动并运行。 So your application may be starting too quickly. 因此,您的应用程序启动速度可能太快。 The Mongo server may not be ready to accept connections yet when your app tries to connect. 当您的应用尝试连接时,Mongo服务器可能尚未准备好接受连接。 Run docker-compose up
, wait for it to fail to start, and then run docker-compose restart service
. 运行docker-compose up
,等待其启动失败,然后运行docker-compose restart service
。
If this fixes it, then you will probably want to make your application retry connecting after some sort of timeout if it fails to connect on start up. 如果此问题得以解决,那么在启动后无法连接时,您可能希望在某种超时后使应用程序重试连接。
Check this out: 看一下这个:
docker-compose.yaml 泊坞窗,compose.yaml
version: '2'
services:
nodejs:
container_name: nodejs
image: nexus.XXXX.com:8000/${BE_BUILD}
restart: always
environment:
- dbhost=mongo
ports:
- "8888:8888"
links:
- mongo
mongo:
container_name: mongo
image: nexus.XXXX.com:8000/doc/docker/proj-mongodb:latest
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- "27017:27017"
And I am successfully able to connect by below URL: 而且我可以通过以下URL成功连接:
const mongoUrl = mongodb://admin:admin@mongo:27017/admin?authSource=admin
authSource
require to the URL. 检查authSource
需要URL。 Just find the issue I do not really understand how does it works ... 只是找到我不太了解它是如何工作的问题...
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
networks:
service-network:
aliases:
- localhost
service:
build:
context: .
container_name: service
restart: always
ports:
- 3012:3012
depends_on:
- mongodb
links:
- mongodb
networks:
app-admin-network:
driver: bridge
Can't see the docker-compose up log but. 但是看不到docker-compose up日志。 had similar problem today. 今天也有类似的问题。 Solved with a small script I have made. 用我制作的一个小脚本解决了。 wait-for-mongo In your dockerfile add this: 等待换蒙戈在你dockerfile补充一点:
RUN npm install -g wait-for-mongodb-slim
## Launch the wait tool and then your application
CMD wait-for-mongo --uri $MONGO_URI --t 2.5 && npm start
where --t is the sceonds until it checks again. --t是场景,直到再次检查为止。
and on your compose add the uri as an environment variable: 并在您的撰写中添加uri作为环境变量:
environment:
MONGO_URI: mongodb://usr:pw@containername:27017/dbname
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.