S
S
Skellord2022-03-30 15:03:36
linux
Skellord, 2022-03-30 15:03:36

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.
624446f9678cd275155899.png
624447001b2f2412435843.png
62444706e0997424433659.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2022-03-30
@leahch

You need to forward port 27017 , not 5000 !

....
     ports:
        - '27017:27017'
....

PS. And don't post pictures. Copy text into code tags , it's that simple. You can’t tear it out of the pictures, but it’s too lazy to type it! That's why I didn't want to answer...

A
Alexander Karabanov, 2022-03-30
@karabanov

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 question

Ask a Question

731 491 924 answers to any question