Docker Container
Create an integration.json file and mount it as a volume:
[
{
"service": "mongodb",
"monitor": true,
"host": "127.0.0.1",
"port": "27017",
"username": "watchlog_monitor",
"password": "your_password",
"authDatabase": "admin",
"tls": false
}
]
Then run the container:
docker run -d \
--name watchlog-agent \
--network host \
-v /path/to/integration.json:/app/app/config/integration.json \
-e WATCHLOG_APIKEY="YOUR_APIKEY" \
-e WATCHLOG_SERVER="https://log.watchlog.io" \
watchlog/agent:latest
mongosh ships inside the agent image, so nothing extra is needed for advanced collection.
Reaching MongoDB from the container
| MongoDB runs… | Use |
|---|---|
On the same host, agent on --network host | 127.0.0.1 |
| On the same host, agent on a bridge network | host.docker.internal (Docker Desktop) or the host's LAN IP |
| In another container on a shared user-defined network | The container or service name, e.g. mongo |
| On a remote host | Its hostname or IP |
If the agent and MongoDB share a Docker network, join it explicitly:
docker run -d \
--name watchlog-agent \
--network app-net \
-v /path/to/integration.json:/app/app/config/integration.json \
-e WATCHLOG_APIKEY="YOUR_APIKEY" \
-e WATCHLOG_SERVER="https://log.watchlog.io" \
watchlog/agent:latest
…and use the service name as the host:
{
"service": "mongodb",
"monitor": true,
"host": "mongo",
"port": "27017",
"username": "watchlog_monitor",
"password": "your_password",
"authDatabase": "admin"
}
Docker Compose
services:
watchlog-agent:
image: watchlog/agent:latest
container_name: watchlog-agent
restart: unless-stopped
environment:
WATCHLOG_APIKEY: "YOUR_APIKEY"
WATCHLOG_SERVER: "https://log.watchlog.io"
volumes:
- ./integration.json:/app/app/config/integration.json:ro
networks:
- app-net
networks:
app-net:
external: true
Creating the monitoring user
If MongoDB itself runs in a container:
docker exec -it mongo mongosh --quiet --eval '
db.getSiblingDB("admin").createUser({
user: "watchlog_monitor",
pwd: "your_password",
roles: [
{ role: "clusterMonitor", db: "admin" },
{ role: "readAnyDatabase", db: "admin" }
]
})
'
Tips
docker exec needs -i to attach stdin. Without it a heredoc is a silent no-op — the command appears to succeed and creates nothing.
Multiple MongoDB Instances
You can monitor multiple MongoDB instances by adding multiple entries with the same service name. Each instance is automatically identified by its host:port combination.
[
{
"service": "mongodb",
"monitor": true,
"host": "mongo-primary",
"port": "27017",
"username": "watchlog_monitor",
"password": "your_password",
"authDatabase": "admin"
},
{
"service": "mongodb",
"monitor": true,
"host": "mongo-analytics",
"port": "27017",
"username": "watchlog_monitor",
"password": "your_password",
"authDatabase": "admin"
}
]
Slow queries
The profiler is off by default and Watchlog never enables it:
docker exec -i mongo mongosh --quiet --eval '
db.getSiblingDB("myapp").setProfilingLevel(1, { slowms: 100 })
'
Then set slowQuery.enabled to true in the mounted config. Nothing needs to be mounted for this — the profiler writes to a collection, which the agent reads over the normal connection.
