Answer the question
In order to leave comments, you need to log in
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 .
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
....
mongoose.connect('mongodb://127.0.0.1:27017/kondrackii', { useNewUrlParser: true, useCreateIndex: true })
....
Answer the question
In order to leave comments, you need to log in
So wrong:
try this:
if you run with docker-compose
this can be removednetwork_mode: host
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question