Home > Net >  docker compose throwing econnrefused
docker compose throwing econnrefused

Time:11-14

While accessing DB it threw me an error that.

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

How to fix it? So my application can make a connection to the database. As in code, you can see my application is relying on multiple databases. how can I make sure before starting the application all of the database containers got started.

version: '3.8'

networks:
  appnetwork:
    driver: bridge

services:
  mysql:
    image: mysql:8.0.27
    restart: always
    command: --init-file /data/application/init.sql
    environment:
      - MYSQL_ROOT_PASSWORD=11999966
      - MYSQL_DATABASE=interview
      - MYSQL_USER=interviewuser
      - MYSQL_PASSWORD=11999966
    ports:
      - 3306:3306
    volumes:
      - db:/var/lib/mysql
      - ./migration/init.sql:/data/application/init.sql
    networks:
      - appnetwork
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.15.2
    restart: always
    ports:
      - 9200:9200
    environment:
      - xpack.security.enabled=false
      - discovery.type=single-node
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536  
    volumes:
      - elastic:/usr/share/elasticsearch/data
    networks:
      - appnetwork
  redis:
    image: redis
    restart: always
    ports:
      - 6379:6379
    volumes:
      - cache:/var/lib/redis
    networks:
      - appnetwork
  mongodb:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    volumes:
      - mongo:/var/lib/mongo
    networks:
      - appnetwork
  app:
    depends_on:
      - mysql
      - elasticsearch
      - redis
      - mongodb
    build: .
    restart: always
    ports:
      - 3000:3000
    networks:
      - appnetwork
    stdin_open: true
    tty: true
    command: npm start
volumes: 
  db:
  elastic:
  cache:
  mongo:

CodePudding user response:

The container (probably app) tries to connect to a mongodb instance running on localhost (i.e. the container itself). Since there is nothing listening on port 27017 of this container, we get the error.

We can fix the probelm by reconfiguring the application running in the container to use the name of the mongodb-container (which, in the given docker-compose.yml, is also mongodb) instead of 127.0.0.1 or localhost.

If we have designed our app accoring to the 12 factors, it should be as simple as setting an environment variable for the container.

CodePudding user response:

Use http://mongo:27017 as connection string instead.

  • Related