Express容器无法通过Docker-Compose连接到Mongo图像

zf9nrax1  于 2022-11-03  发布在  Docker
关注(0)|答案(2)|浏览(236)

我尝试用docker compose将一个express容器连接到MongoDB图像,但连接被拒绝,我可以用robomongo连接到数据库。我无法了解发生了什么,这是连接它的express代码:

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

这是停靠合成文件

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)]: {} }
xytpbqjk

xytpbqjk1#

在Docker容器中运行的每个进程都认为自己是“世界上唯一的一个”。这意味着对于这个进程,localhost意味着:我的,容器是localhost.而你的后端是单独在他的容器中,所以这就是为什么他在localhost下找不到mongodb.
要解决这个问题,你应该把主机名“mongodb”而不是“localhost”,因为在docker-compose中,你可以使用它们的名称访问服务-这意味着mongodb容器也可以使用“backend”域访问你的后端。
还请注意,“links”在docker中已被弃用,不应使用-在您的配置中不需要它,因为docker-compose使用上面提到的方法为docker-compose文件中的每个服务提供相互访问。

dgtucam1

dgtucam12#

Jakub Bujny的回答是当场的,我不明白,因为它没有包括一个例子。所以我添加一个。

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

相关问题