W
W
wasserbord2018-05-19 00:25:07
linux
wasserbord, 2018-05-19 00:25:07

Why can't MongoDB normally "throw" into the Docker container from the host?

Host OS is Ubuntu.
Pay attention in which direction I want to "forward mongo",
So, mongo listens on the host - but you need to connect to it from the container.
At the same time, the solution should not be "indiscriminate", because in the same application there is also another one connected to the network (though other ports), and for example --net=host just spoils the whole thing. The decision should, if possible, affect only mongu.
I've been struggling with this problem for many hours now. The volume of hemorrhoids is a little amazing.
For example, at first I tried to connect with the mongo client (mongocxx) via localhost, and bind localhost to the desired IP via hosts (in the container, of course). But this idea just failed. The fact is that --add-host does not work properly, it only adds an entry to hosts, but does not remove duplicates, which are exactly the same for localhost. And any attempts to set up hosts during the build, before starting the container, fail, because for some reason it is replaced at startup.
Why did the venerable docker developers come up with such --add-host - I don’t understand.
Further. In the end, I decided to just accurately determine the desired IP - and for this I wrote my client and server in python and experimented with them.
Result: this IP is 172.17.0.1 .
Those. if in docker the client connects to 172.17.0.1, and the server on the host listens to 0.0.0.0, then everything is ok.
So it was with my self-written client-server.
And I decided to raise the mongo to 0.0.0.0, and connect it directly to 172.17.0.1 for now , and then it will be clear how to attach the localhost here - through iptables, etc.
But alas - even that didn't work!
Then I killed the mongi service, set up my python server on the mongi port , ran the prog with mongocxx in the docker - and what do you think? Accepts are coming!
Killed python server, started mongo again - error, can't connect. Again python server - again accept'y come.
And with a real monga - it does not work. What is she missing...
I watch netstat -tlnp on the host in both cases - the same thing... 0.0.0.0.
... It is clear that you can dig even further - sniff all TCP segments in both cases, parse by bytes, think, try more options ... But, perhaps, someone will give an answer faster?
UPD: Just in case, uri: "mongodb://172.17.0.1:27017/Loremipsum", mongocxx::uri is created from it, and mongocxx::client from it, without extra parameters.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Tsvetkov, 2018-05-19
@yellow79

Probably your monga wants to connect to it only from localhost, try picking its configs to change bindIp or bindIpAll

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question