MongoDB connection error with localhost

Hello there!

I face a problem with connecting my MongoDB (version 4.4.2).

I run it without Docker and I try to connect it with a docker instance of Metabase on the same hardware.

When I try to connect it and write localhost as the host value I get the following error.

Database name : Timed out after 3000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]

When I paste the router’s ip, it connects properly. For obvious reasons I would love to use the localhost and not the router’s ip address.

Anyone facing the same issue than can help me solve this?

Hi @costaNull
Without knowing your setup or how you are running Metabase, then it’s difficult to know, but you are likely not listening on the correct interfaces.
A container is just like having separate servers, so unless you connect them, then they cannot see each other.
Inside a container, localhost is the container itself - not the host or another container.

Hey @flamber you are right!

What is the proper way to connect the Metabase Docker image with the mongo server (not running with docker).

@costaNull I would not recommend using Docker unless you completely understand how it functions - it complicates everything a lot more. https://www.metabase.com/blog/how-to-run-metabase-in-production/index.html
You would use host.docker.internal: https://stackoverflow.com/questions/28056522/access-host-database-from-a-docker-container

Got it! Thanks for the fast response!