A
A
Artem Kondratsky2019-07-13 22:03:54
MongoDB
Artem Kondratsky, 2019-07-13 22:03:54

How to forward port from MongoDB container to NodeJS container in Docker?

There are two containers , one with NodeJS BackEnd and the other with MongoDB .
Everything works, but the NodeJS application does not see the MongoDB port , logically, these are different containers. Rummaging around the Internet, I found a seemingly suitable solution - add the network_mode: host line to the file .

docker-compose.yml file

version: '3.7'
services:
  app:
    image: node:latest
    container_name: kondrackii-backend
    build: ./backend
    ports:
      - '8081:8081'
    volumes:
      - "./backend:/usr/src/app"
      - "/usr/src/app/node_modules"
    depends_on:
      - mongo
    #links:
    #  - mongo
    network_mode: host
    command: 'npm start'
  mongo:
    container_name: mongo
    image: mongo:latest
    ports:
      - '27017:27017'
    command: 'mongod'
    restart: always
    volumes:
      - "/var/www/kondrackii.ru/db:/data/db"
    network_mode: host


Part of a MongoDB connection file

....
mongoose.connect('mongodb://127.0.0.1:27017/kondrackii', { useNewUrlParser: true, useCreateIndex: true })
....


This option doesn't work, NodeJS still does n't see MongoDB . What to do?
I'm already thinking about putting a NodeJS application and MongoDB in one container. Is it right to do so?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2019-07-13
@kondrackii

So wrong:
try this:
if you run with docker-compose
this can be removed
network_mode: host

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question