【问题标题】:Express container unable to connect to Mongo Image with Docker-ComposeExpress 容器无法使用 Docker-Compose 连接到 Mongo Image
【发布时间】:2018-09-04 20:31:14
【问题描述】:

我正在尝试使用 docker compose 将 express 容器连接到 MongoDB 映像,但连接被拒绝,我可以使用 robomongo 连接到数据库。我不明白发生了什么,这是连接它的快速代码:

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】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 2021-06-04
    • 2018-09-02
    • 2016-12-31
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多