Trouble connecting to Replica set in container from another container

I am having trouble connecting to a Dockerized MongoDB single-node replica set. The container which I am connecting from is not in the same Docker network, and is running a Python application. I can successfully connect locally using the connection string mongodb://localhost:27017/?replicaSet=rs0 but using mongodb://host.docker.internal:27017/?replicaSet=rs0 does not work from within the app container. However, connecting directly works fine from within the container using: mongodb://host.docker.internal:27017/?directConnection=true. For reference, here is my docker-compose.yml:

version: '3'
services:
  mongodb-local-rs:
    container_name: mongodb-local-rs
    hostname: mongodb-local-rs
    image: mongo:latest
    environment:
      MONGO_INITDB_DATABASE: mongodb_local
    ports:
      - 27017:27017
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./mongodb_data_volume:/data/db:rw
      - ./scripts/mongo-init/rs-init.js:/docker-entrypoint-initdb.d/a-rs-init.js:ro
      - ./scripts/mongo-init/mongo-init.js:/docker-entrypoint-initdb.d/b-db-init.js:ro
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
volumes:
  mongodb_data_volume:
    driver: local

where the second volume contains the file for replicaset configuration:

var cfg = {
    _id: "rs0",
    members: [
        {
            _id: 1, 
            host: "localhost:27017"
        }
    ]
};

rs.initiate(cfg)

The errors looks as follows: Could not reach any servers in [('localhost', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 30s, Topology Description: <TopologyDescription id: 657e0e1f5a6f497177813533, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>

Any help in this regard would be greatly appreciated! Thanks :slight_smile:

Hi @Vanessa_Mohr and welcome to MongoDB community forums!!

The above issue seems to be a general issue where you are trying to establish network between two different containers.
As you might already know that, the docker containers run independently on the machine without knowing about the other containers on the same machine. To connect them together, we perform networking.

To make the two containers communicate, you can amend the docker-compose file by following the documentation on docker-compose in the services section of the yaml file.

Below is the same amended docker file that you can use:

version: '3'
services:
  mongodb-local-rs:
    container_name: mongodb-local-rs
    hostname: mongodb-local-rs
    image: mongo:latest
    environment:
      MONGO_INITDB_DATABASE: mongodb_local
    ports:
      - 27017:27017
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./mongodb_data_volume:/data/db:rw
      - ./scripts/mongo-init/rs-init.js:/docker-entrypoint-initdb.d/a-rs-init.js:ro
      - ./scripts/mongo-init/mongo-init.js:/docker-entrypoint-initdb.d/b-db-init.js:ro
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
    networks:
      - mynetwork  # Add a network to connect services

  python-app:
    container_name: python-app
    build:
      context: ./path/to/your/python/app  # Replace with the actual path
    depends_on:
      - mongodb-local-rs
    networks:
      - mynetwork  # Use the same network as MongoDB
    # Add other configurations for your Python app

networks:
  mynetwork:
    driver: bridge

to make the connection.

Let us know if you are still facing the issues.

Regards
Aasawari

1 Like

Hi there - thank you so much for your suggestion! I used the docker compose you sent (adding both the MongoDB and python app in the same network) and used the connection string “mongodb://:27017/?replicaSet=rs0” and I’m getting the error

pymongo.errors.ServerSelectionTimeoutError: <python-app-service-name>:27017: [Errno 111] Connection refused (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 65814d8abeb53a24df032377, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('<python-app-service-name>', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('postman_pat:27017: [Errno 111] Connection refused (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>