0

I am facing weird issue while connecting MongoDB running in a separate container from my nodejs container, it displays the following error while trying to connect to MongoDB. My Dockerfile

    FROM node:latest

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8000

CMD ["npm","start"]
enter code here

docker-compose

version: '3'
services:
  web:
    build: .
    ports:
     - "8000:8000"
    links:
    - mongo
    - redis

  mongo:
    image: mongo
    ports:
      - "49155:49155"
  redis:
    image: "redis:alpine"

mongo config

mongoose: { // MongoDB
    // uri: mongodb://username:password@host:port/database?options
    uri: `mongodb://localhost:27017/${DB_NAME}`,
    options: {
    },
    seed: {
      path: '/api/models/seeds/',
      list: [
        {
          file: 'user.seed',
          schema: 'User',
          plant: 'once' //  once - always - never
        },
        {
          file: 'example.seed',
          schema: 'Example',
          plant: 'once'
        }
      ]
    },
  },

Issue enter image description here

I am only studied docker pls help me

2 Answers 2

1

On creating a new container, docker will attach that container to a default internal bridge. https://docs.docker.com/network/ check network drivers. To make it available for the localhost you will have to set

network_mode: "host", in docker-compose in mongo section

Sign up to request clarification or add additional context in comments.

Comments

0

indrajeet's answer will work, but since you are venturing into docker-compose, it is better if you treat each of your services/container as a host. In your case, the error is because your web app is trying to connect to a mongo service running on the same (web) localhost.

Instead you should connect to the mongo service/contaner, which docker-compose conveniently let your web container to access the mongo service/container/host via the hostname "mongo" ( the service name you specified in the docker-compose yaml)

My answer is to change the uri line to

uri: `mongodb://mongo:27017/${DB_NAME}`,

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.