Mailgraph Docker Container
Postfix is probably still one of the most widely used free SMTP servers. For those who self-host an email server and want to keep track of the volume of incoming and outgoing emails, they quickly come across the tool Mailgraph by David Schweikert. Mailgraph consists of a Perl script that analyzes the log files of Postfix, populates an RRDtool database, and a CGI script that presents the data in the form of graphics in a clear manner in the browser.
In addition to tracking the number of sent and received emails, there is also a graphic representation for spam and viruses.
Thanks to a patch by Sebastian van de Meer, Mailgraph also provides information on whether incoming servers support SPF, DMARC, or DKIM. For this to work, it’s necessary to have these mechanisms configured on your Postfix instance.
Docker Container
To avoid installing Mailgraph and the corresponding web server directly on your email server, you can certainly run it in a Docker container.
For this purpose, I have created a Dockerfile that includes all the dependencies and can be used immediately. You can specify the path to the mail log file and a folder for storing the RRD files as volumes. Alternatively, you can also use a Docker volume for this purpose.
You can use the -v
flag to mount host paths or volumes into the container: -v [Host-Path]:[Container-Path]
.
The log file is expected within the container at the path /var/log/mail/mail.log
, and the RRD files are located at /var/www/mailgraph/rrd/
.
The image defaults to serving the Mailgraph website on port 80 and the path /mailgraph
.
docker run --rm \
-v /var/log/mail/mail.log:/var/log/mail/mail.log \
-v /var/data/mailgraph/rrd/:/var/www/mailgraph/rrd/ \
davidullrich/mailgraph:latest
If you now access http://localhost:80/mailgraph/ in your browser, you will see the initial graphs displayed.
Docker Compose
If you’re using Docker Compose for orchestration, a corresponding configuration might look something like this:
version: '3'
services:
mailgraph:
image: davidullrich/mailgraph:latest
hostname: mail.example.com
volumes:
- /var/log/mail/mail.log:/var/log/mail/mail.log
- /var/data/mailgraph/rrd/:/var/www/mailgraph/rrd/
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
Reverse Proxy with Traefik
If you don’t want your Mailgraph webpage to be publicly accessible, you can set up authentication using a reverse proxy. An easy way to do this is by using Traefik, a reverse proxy that integrates well with Docker Compose. Once you have Traefik set up on your system, configuring Mailgraph through labels becomes straightforward.
version: '3'
services:
mailgraph:
image: davidullrich/mailgraph:latest
hostname: mail.example.com
volumes:
- /var/log/mail/mail.log:/var/log/mail/mail.log
- /var/data/mailgraph/rrd/:/var/www/mailgraph/rrd/
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.mailgraph-router.rule=Host(`mail.example.com`) && PathPrefix(`/mailgraph`)"
- "traefik.http.routers.mailgraph-router.entryPoints=websecure"
- "traefik.http.routers.mailgraph-router.service=mailgraph-service"
- "traefik.http.services.mailgraph-service.loadBalancer.server.scheme=http"
- "traefik.http.services.mailgraph-service.loadBalancer.server.port=80"
- "traefik.http.routers.mailgraph-router.middlewares=mailgraph-middleware-auth"
- "traefik.http.middlewares.mailgraph-middleware-auth.basicauth.users=user:[password-hash]"
Dovecot Extension
As I use Dovecot as my IMAP server, I’ve extended Mailgraph to display the number of successful and failed IMAP logins.
Source Code and Docker Image
You can find the source code for the Docker image on GitHub, and the pre-built image is available on Docker Hub.