suchcodemuchwow
suchcodemuchwow

Reputation: 1056

Cannot connect to mongodb container

Hi I'm trying to develop simple api but I couldn't establish connection between api and mongodb. When I send a query to database it hangs and couldn't get response. I am trying to connect to mongo container with this code:

mongoose.connect("mongodb://mongo/micro-linkedin_api_1");

Here is my docker-compose file:

version: "3"

services:
  api:
    restart: always
    build: ./api
    ports:
      - "3000:3000"
    links:
      - mongodb
    env_file:
      - ./api/.env
    volumes:
      - ./api:/usr/xd/job-bot/api
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
  scraper:
    build: ./scraper

and my folder structure is like this one :

Folder Structure

Upvotes: 0

Views: 104

Answers (1)

Tzook Bar Noy
Tzook Bar Noy

Reputation: 11677

As I can see in docker-compose you defined 2 services:

api and mongodb

in Mongoose docs, this is how you connect:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

You did:

mongoose.connect("mongodb://mongo/micro-linkedin_api_1");

so the host is not correct, you used mongo instead of mongodb as defined in docker-compose services.

Upvotes: 1

Related Questions