docker撰写文件

version: '3'
services:
  web:
    build:
      context: ./reactapp
    ports:
      - "3000:3000"
  server:
    build:
      context: ./expressapp
    ports:
      - "4000:4000"
  mongo:
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"

节点js代码
function connectToMongoDB() {
    return mongoose.connect('mongodb://127.0.0.1:27017').then(() => {

    }, (error) => {
        console.log(error);
        setTimeout(() => {
            connectToMongoDB()
        }, 10000);
    });
}

最佳答案

您必须使用“mongo”作为主机名,而不是127.0.0.1

10-07 13:27