0

I have an up and running mongodb container which looks like this :

version: "3.8"
services:
  mongodb:
    image : mongo
    container_name: mongodb
    environment:
    - PUID=1000
    - PGID=1000
    - MONGO_INITDB_ROOT_USERNAME=admin
    - MONGO_INITDB_ROOT_PASSWORD=admin123#
    volumes:
    - C:\Users\Admin\mongo_db_payslip\database:/data/db
    ports:
    - 27017:27017
    restart: unless-stopped 

I have another compose container which is running as well whose compose file looks like this :

version: "3"
services:
  rabbit:
      image: rabbitmq:3-management
      restart: always
      ports:
      - 15672:15672
      - 5672:5672
      networks:
      - rabbit_network
      env_file:
      - ./.env
  api_container:
      build: ./api_container
      command: gunicorn --certfile=cert.pem --keyfile=key.pem app:app --bind 0.0.0.0:5000
      restart: always
      networks:
      - rabbit_network
      ports:
      - 443:5000
      depends_on:
      - rabbit
      env_file:
      - ./.env
  completion_consumer:
      build: ./completion_consumer_container 
      restart: always
      networks:
      - rabbit_network
      depends_on:
        - rabbit
      env_file:
      - ./.env
networks:
  rabbit_network:
    driver: bridge

The code within completion-container tries to access DB. It was working as long as mongodb was not a container. After switching to container, I am getting following error :

pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 63563ec7ead36e31ccc97935, topology_type: Single, servers: [<ServerDescription ('127.0.0.1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 111] Connection refused')>]>

Both of these services runs on my local machine How can i resolve the above error ?

Naveen Gabriel
  • 679
  • 2
  • 9
  • 25
  • The special IP address 127.0.0.1 in Docker almost always refers to the current _container_. You need to arrange for both Compose files to use the same Docker network, and then the Compose service name `mongodb` will be usable as a host name; the linked question describes this setup further. – David Maze Oct 24 '22 at 09:17

0 Answers0