Express容器无法使用Docker-Compose连接到Mongo Image

问题描述 投票:0回答:1

我正在尝试使用docker compose将快速容器连接到MongoDB映像但是连接被拒绝,我可以使用robomongo连接到db。我无法得到正在发生的事情,这是连接它的快速代码:

mongoose.connect('mongodb://localhost:27017/database')
.then(()=>console.log('connection succesfull to url'))
.catch((err)=>console.error(err));

这是docker-compose文件

version: "3"

services:
    backend:
        build:
            context: ../backend
            dockerfile: ${PWD}/images/backend/Dockerfile
        container_name: backend
        ports:
            - "${BACKEND_PORT}:${BACKEND_PORT}"
        env_file:
            - ./deploy.env            
        environment:
            -  PORT=3000
            -  MONGO_CONNECTION=${MONGO_CONNECTION}
        command: npm start
        links:
            - mongodb
        depends_on:
            - mongodb               
    front-app:
        build:
            context: ../front-app
            dockerfile: ${PWD}/images/angular/Dockerfile
        container_name: front-app
        ports:
            - "${FRONTEND_PORT}:4200"
        env_file:
            - ./deploy.env            
        command: npm start
    mongodb:
        image: mongo:3.6
        container_name: mongo
        volumes:
            - "${MONGO_DB_DATA}:/data/db"
            - "${MONGO_DB_DATA}:/data/configdb"
        ports:
            - "27017:27017"

这是错误

backend      | { MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
backend      |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
backend      |     at Pool.emit (events.js:182:13)
backend      |     at Pool.EventEmitter.emit (domain.js:442:20)
backend      |     at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
backend      |     at Object.onceWrapper (events.js:273:13)
backend      |     at Connection.emit (events.js:182:13)
backend      |     at Connection.EventEmitter.emit (domain.js:442:20)
backend      |     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
backend      |     at Object.onceWrapper (events.js:273:13)
backend      |     at Socket.emit (events.js:182:13)
backend      |     at Socket.EventEmitter.emit (domain.js:442:20)
backend      |     at emitErrorNT (internal/streams/destroy.js:82:8)
backend      |     at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
backend      |     at process._tickCallback (internal/process/next_tick.js:63:19)
backend      |   name: 'MongoNetworkError',
backend      |   errorLabels: [ 'TransientTransactionError' ],
backend      |   [Symbol(mongoErrorContextSymbol)]: {} }
mongodb express docker docker-compose
1个回答
0
投票

在docker容器中运行的每个进程都认为他是“世界上唯一的进程”。这意味着对于这个过程,localhost意味着:我的容器的localhost。并且你的后端独自在他的容器中,这就是为什么他在localhost下找不到mongodb的原因。

要解决这个问题,你应该放置主机名“mongodb”而不是“localhost”,因为在docker-compose中你可以使用他们的名字访问服务 - 这意味着mongodb容器也可以使用“后端”域访问你的后端。

另请注意,“链接”在docker中已弃用,不应使用 - 在配置中不需要它,因为docker-compose使用上述方法为docker-compose文件中的每个服务提供相互访问权限。

© www.soinside.com 2019 - 2024. All rights reserved.