简体   繁体   English

将 docker-compose 中的 MongoDB 单节点副本集公开到 macOS 上的主机

[英]Expose MongoDB single-node replica set inside docker-compose to host on macOS

I have this docker-compose file:我有这个 docker-compose 文件:

version: "3"
services:
  mongo:
    image: bitnami/mongodb:4.4
    restart: always
    ports:
      - 27017:27017
    volumes:
      - ./db:/bitnami/mongodb
    environment:
        - MONGODB_REPLICA_SET_MODE=primary
        - ALLOW_EMPTY_PASSWORD=yes

which should expose the MongoDB replica set to the outer host via mongodb://localhost:27017 .这应该通过mongodb://localhost:27017将 MongoDB 副本集公开给外部主机。 However I am getting this error message:但是我收到此错误消息:

Server selection timeout: None of the available servers suitable for criteria Predicate. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address: mongo:27017, Type: RsGhost, Average RTT: 10.349064ms, Last Update Time: DateTime(OffsetDateTime { utc_datetime: PrimitiveDateTime { date: Date { year: 2022, ordinal: 197 }, time: Time { hour: 21, minute: 14, second: 38, nanosecond: 986000000 } }, offset: UtcOffset { hours: 0, minutes: 0, seconds: 0 } }), Max Wire Version: 9, Min Wire Version: 0 }, ] }

How do I expose the replica set to the outer host?如何将副本集暴露给外部主机? I've tried adding 127.0.0.1 mongo to my /etc/hosts with no luck.我尝试将127.0.0.1 mongo添加到我的/etc/hosts中,但没有成功。 I am on macOS 12.4 and Docker Desktop 4.10.1.我在 macOS 12.4 和 Docker Desktop 4.10.1 上。

Localhost (127.0.0.1) is your problem.本地主机(127.0.0.1)是你的问题。 Only clients what are in side that mongod -pod can connect, but as we know, in that pod there is only mongodb and client(s) are in the different pods.只有 mongod -pod 内部的客户端才能连接,但正如我们所知,在该 pod 中只有 mongodb 并且客户端位于不同的 pod 中。

You need to use " Docker container networking "您需要使用“ Docker 容器网络

Create a network ($ docker network create app-tier --driver bridge) and then configure your mongodb (compose -file) to use it:创建一个网络($ docker network create app-tier --driver bridge),然后配置您的 mongodb(compose -file)以使用它:

networks:
  app-tier:
    driver: bridge

services:
  mongodb:
    image: 'bitnami/mongodb:4.4'
    networks:
      - app-tier

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

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