简体   繁体   English

猫鼬无法在 docker-compose 中连接

[英]mongoose failing to connect in docker-compose

I've got a simple docker-compose.yml that looks like this:我有一个简单的 docker-compose.yml,如下所示:

services:
  mongodb-service:
    image: mongo:latest
    command: "mongod --bind_ip_all"
  mongodb-seeder:
    image: mongo:latest
      depends_on:
        - mongodb-service
      command: "mongoimport --uri mongodb://mongodb-service:27017/auth --drop --file /tmp/users.json"
  myapp:
    image: myapp:latest
    environment:
      DATABASEURL: mongodb://mongodb-service:27017/auth
    depends_on:
      - mongodb-service

myapp is a nodejs app that uses mongoose to connect to a mongodb database like so: myapp是一个 nodejs 应用程序,它使用 mongoose 连接到 mongodb 数据库,如下所示:

const databaseURL = process.env.DATABASEURL;
async function connectToMongo() {
    try {
        return await mongoose.connect(databaseURL, {
            useUnifiedTopology: true,
            useNewUrlParser: true,
            useCreateIndex: true,
        });
    }
    catch (error) {
        logger.error('MongoDB connect failure.', error);
    }
}

mongodb-seeder works perfectly fine. mongodb-seeder工作得很好。 I can kick it off, it connects to mongodb-service and runs the import without any problems.我可以启动它,它连接到mongodb-service并运行导入没有任何问题。 However, myapp starts up, tries to connect to mongodb-service for 30 seconds, then dies:但是, myapp启动,尝试连接到mongodb-service 30 秒,然后死亡:

ERROR 2020-09-16T12:13:21.427 [MAIN] MongoDB connection error! [Arguments] {
  '0': MongooseError [MongooseTimeoutError]: Server selection timed out after 30000 ms
      ...stacktrace snipped...
      at Function.Module._load (internal/modules/cjs/loader.js:724:14) {
    message: 'Server selection timed out after 30000 ms',
    name: 'MongooseTimeoutError',
    reason: Error: connect ECONNREFUSED 127.0.0.1:27017
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
      name: 'MongoNetworkError',
      [Symbol(mongoErrorContextSymbol)]: {}
    },
    [Symbol(mongoErrorContextSymbol)]: {}
  }
}

Note: The IP address in this log says it tried to connect to 127.0.0.1 , not mongodb://mongodb-service:27017/auth .注意:此日志中的 IP 地址表示它尝试连接到127.0.0.1而不是mongodb://mongodb-service:27017/auth No matter what value I put in for DATABASEURL , it keeps printing 127.0.0.1 .无论我为DATABASEURL输入什么值,它都会继续打印127.0.0.1 Any ideas why I can't get mongoose to recognize the hostname I'm giving it?任何想法为什么我不能让猫鼬识别我给它的主机名? And why would mongoose not be able to connect to a service that's clearly network-visible, since another container ( mongodb-seeder ) can see it without any problems?为什么 mongoose 无法连接到网络可见的服务,因为另一个容器( mongodb-seeder )可以毫无问题地看到它?

Edit: I'm using mongoose 5.8.7编辑:我使用猫鼬 5.8.7

I was able to solve this on my own, turns out it was a pretty stupid miss on my part.我能够自己解决这个问题,结果证明这是我的一个非常愚蠢的失误。

The DockerFile for my app defined an entrypoint that executed a script that exported the DATABASEURL prior to running.我的应用程序的DockerFile定义了一个入口点,该入口点执行一个在运行之前导出 DATABASEURL 的脚本。 Removing that export allowed my environment: settings to flow down to the nodejs app.删除该导出允许我的environment:设置流向 nodejs 应用程序。

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

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