如何通过Docker容器为Grafana安装mongodb-grafana插件?
Since you already know how to set up this plugin on bare-metal Linux, getting it working in a Docker container just requires a couple of container-specific adjustments. Let's cover two reliable methods: building a custom Grafana image (great for production), and mounting a local plugin directory (perfect for quick testing).
Method 1: Build a Custom Grafana Image (Recommended for Production)
This approach bakes the plugin directly into your Grafana image, so you don't have to fuss with mounting directories every time you spin up a container.
- Create a
Dockerfilein a new directory:
# Start with the official Grafana base image FROM grafana/grafana:latest # Install the MongoDB datasource plugin using Grafana's CLI tool RUN grafana-cli plugins install jamesosgood-mongodb-datasource
- Build your custom Grafana image with the plugin included:
docker build -t my-grafana-with-mongodb .
- Run the container using your newly built image:
docker run -d -p 3000:3000 my-grafana-with-mongodb
Method 2: Mount a Local Plugin Directory (Quick Testing)
If you don't want to build a new image, you can install the plugin to a local directory and mount it into the Grafana container.
- Create a local directory to store Grafana plugins:
mkdir -p ./grafana-plugins
- Use a temporary Grafana container to install the plugin into your local directory:
docker run --rm -v ./grafana-plugins:/var/lib/grafana/plugins grafana/grafana:latest grafana-cli plugins install jamesosgood-mongodb-datasource
- Fix directory permissions (Grafana runs as UID 472 inside the container, so we need to match that locally):
chown -R 472:472 ./grafana-plugins
- Start your persistent Grafana container with the plugin directory mounted:
docker run -d -p 3000:3000 -v ./grafana-plugins:/var/lib/grafana/plugins grafana/grafana:latest
Verify the Installation
Once your container is up and running, log into Grafana (default credentials: admin/admin), head to Configuration > Data Sources, and you should see "MongoDB" listed as an available datasource. You're all set to start building dashboards for your MongoDB data!
内容的提问来源于stack exchange,提问作者Ryan Wegmeier

