Cannot connect to MongoDB in docker
Solution 1
You have to tell the container to use it's own IP Address instead of localhost.
For example, let's assume you generated scaffold code with expressjs
, you have to write in routes/index.js
var mongodb = require('mongodb');
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongodb.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://172.17.0.5:27017/dbname';
// Connect to the server
MongoClient.connect(url, function (err, db) {
.........
where 172.17.0.5
is the $CONTAINER_IP
you can find the container ip via
$ docker inspect $CONTAINER_NAME | grep IPAddress
If no internet in container
Open with sudo sudo vim /etc/NetworkManager/NetworkManager.conf
and comment out the line dns=dnsmasq
, so you would now have:
[main]
plugins=ifupdown,keyfile,ofono
#dns=dnsmasq
[ifupdown]
managed=false
Edit: service name instead of IP
You may now use service name instead of IP Address of the container, eg:
// Define where the MongoDB server is
var url = 'mongodb://api:27017/dbname';
Solution 2
The gotcha here is - if a container listens on 127.0.0.1, then the host can't map ports to it.
You need to listen on 0.0.0.0
or similar, and then the -p
can reach the container's interface.
Solution 3
I had the same problem but found another way to address this issue. You can just pass network parameter while running docker image and this way docker points to correct localhost.
docker run --network="host" ....
Related videos on Youtube
wheeler
Updated on September 18, 2022Comments
-
wheeler almost 2 years
Using the official
mongo
image from the docker hub, I run the following command to get it running:docker run --name api -p 127.0.0.1:27017:27017 -p 127.0.0.1:28017:28017 -d mongo
Then from another terminal shell, I run
mongo
However, I keep getting the following error:
MongoDB shell version: 3.2.3 connecting to: test 2016-02-18T13:52:08.110-0700 I NETWORK [thread1] Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017 2016-02-18T13:52:08.110-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017] 2016-02-18T13:52:08.110-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017' : connect@src/mongo/shell/mongo.js:224:14 @(connect):1:6 exception: connect failed
I checked the logs for the container, and everything seemed right:
2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=73334e5089e1 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] db version v3.2.3 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] git version: b326ba837cf6f49d65c2f85e1b70f6f31ece7937 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] allocator: tcmalloc 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] modules: none 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] build environment: 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] distmod: debian71 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] distarch: x86_64 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] target_arch: x86_64 2016-02-18T20:51:20.538+0000 I CONTROL [initandlisten] options: {} 2016-02-18T20:51:20.542+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] 2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] 2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2016-02-18T20:51:20.615+0000 I CONTROL [initandlisten] 2016-02-18T20:51:20.616+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data' 2016-02-18T20:51:20.616+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker 2016-02-18T20:51:20.633+0000 I NETWORK [initandlisten] waiting for connections on port 27017
What's wrong with docker or Mongo? I've searched everywhere for an answer, and nothing has worked so far. Is docker or Mongo broken?
Edit: I did a little more investigation, and I found that I cannot access the internet from inside the container. I connect to it by running
docker exec -ti api /bin/bash
and then try to ping google or something, and the command just hangs.I can only seem to get an internet connection from the container to the outside world by using
--net=host
in thedocker run
command.-
Ben Reeves almost 8 yearsThis may not help, but I noticed in the mongo client error message that it said
network error while attempting to run command 'isMaster' on host '127.0.0.1:27017'
. Perhaps this is a permission error? -
wheeler almost 8 yearsIt turned out to be a docker networking issue with docker and my VPN client conflicting with each other.
-
-
wheeler over 8 yearsSo you mean modify the
docker run
command to bedocker run --name mongo -p 0.0.0.0:27017:27017 -p 0.0.0.0:28017:28017 -d mongo
instead? If so, that doesn't change anything, I still cannot connect to the container using the mongo shell. -
wheeler over 8 yearsThat didn't work, but what did work was disabling dnsmasq on the host.
-
Saúl Martínez Vidals over 7 yearsthis worked for me, I just commented the line
bindIp:
under thenet:
section, thank you ! -
rezwits about 6 yearsapi:27017, uri scheme for the win...
-
technazi about 5 yearsWhat is a service name?
-
Daniel Andrei Mincă about 5 yearsthis is a service name
-
Timothy Pulliam almost 5 yearsHow would you know that container IP ahead of time? Say, if you wanted to put this in a Dockerfile?
-
Daniel Andrei Mincă almost 5 yearsYou don't. The IPs are assigned automatically by default from a class C range. You can a) get the IP after container is up via
docker inspect <container>
; or b) start a container with static IP address assigned - requires creating adocker network
, there's alot on this over the Internet.