How to connect Grafana (via Docker) to MongoDB in a VM without exposing port 27017 to the open internet?

Hello,

I have a personal project where I use Grafana Cloud for monitoring, and MongoDB is running in a Docker Compose setup within a VM. In order for Grafana Cloud to connect to MongoDB, I need to expose port 27017 of MongoDB to the open internet, which raises a security concern as it potentially makes the database vulnerable to attacks.

Additionally, I tried running Grafana locally on the same VM (via Docker) to connect directly to MongoDB, but I found that in the standard version of Grafana, I can’t connect to MongoDB. It seems the only way to achieve this connection is by using Grafana Enterprise.

I would like to know if there is any alternative to:

  1. Connect Grafana (Open version, via Docker) to MongoDB in my VM without having to expose port 27017 to the open internet.
  2. Keep MongoDB secure without opening the port directly to the internet.

I’ve explored some options, like configuring the Docker network for internal communication, but I haven’t found a viable solution yet.

Thanks in advance for your help!

if both your mongo and grafana containers are in the same network, you should just be able to use the internal docker ip and port, without exposing mongo to the outside world. see snip below:

my grafana, postgres, mysql etc. are all on the same network, 172.18.0.x, postgres and mysql do not have their ports opened, but grafana can access them on the standard port, because it is inside the same network…

Use private datasource connect which allows you to add in an agent locally which creates a private tunnel with grafana cloud; Private data source connect (PDC) | Grafana Cloud documentation to read more. Mongodb datasource is supported with PDC.