Answer the question
In order to leave comments, you need to log in
Why is there no access to MongoDB from a docker container?
Hello. I am trying to connect from containerized node js app to local mongodb. However, when connecting, an error occurs (Fig. 1). The database service is running and I can connect to it (Figure 2). To start the container, I use this config (Figure 3). I note that this config works fine on windows, problems arose on ubuntu lts.
Answer the question
In order to leave comments, you need to log in
You need to forward port 27017 , not 5000 !
....
ports:
- '27017:27017'
....
The fact is that each container has its own loopback interface with IP 127.0.0.1 inside.
There are two ways to achieve what you want.
First, remove isolation from the network ( network_mode: host ), then everything will just work, but you don’t need to do this because no one will allow you to do this on the market.
Second, you need to make the mongo listen either on 0.0.0.0 (that is, on all interfaces) and connect to it from the container using the gateway IP (you can see it in the output docker inspect <container_name>
), or make the mongo listen on exactly one IP which is the gateway for the container.
PS
And yes, screenshots of the text are really bad manners - don't do it like that.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question