How to Configure Mail Sending from Within a Docker Container for Backup Alerts


6 views

When running monitoring scripts inside Docker containers, getting email notifications working can be surprisingly tricky. The standard mailutils approach that works on regular Linux servers often fails silently in containers due to missing configurations and security restrictions.

Here's a reliable approach using SSMTP (a simpler alternative to Postfix):

FROM ubuntu:latest
RUN apt-get update && \
    apt-get install -y ssmtp mailutils && \
    apt-get clean

# Configure SSMTP to use Gmail's SMTP server
RUN echo "root=your@gmail.com" >> /etc/ssmtp/ssmtp.conf && \
    echo "mailhub=smtp.gmail.com:587" >> /etc/ssmtp/ssmtp.conf && \
    echo "AuthUser=your@gmail.com" >> /etc/ssmtp/ssmtp.conf && \
    echo "AuthPass=your-app-password" >> /etc/ssmtp/ssmtp.conf && \
    echo "UseTLS=YES" >> /etc/ssmtp/ssmtp.conf && \
    echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf

ADD disk-alert.sh /
CMD ["/disk-alert.sh"]

For production environments, consider connecting to an external SMTP server:

# In your script instead of mail command:
curl -s --ssl-reqd \
  --url 'smtps://smtp.example.com:465' \
  --user 'user:pass' \
  --mail-from 'alerts@yourdomain.com' \
  --mail-rcpt 'admin@yourdomain.com' \
  --upload-file - << EOF
From: Docker Alert 
To: Admin 
Subject: Disk Space Alert

Backup server remaining free space is critically low. Used: ${CURRENT}%
EOF

Before deploying, test your email setup interactively:

docker run -it --rm your-image bash
echo "Test email body" | mail -s "Test Subject" recipient@example.com

Never hardcode email credentials in Dockerfiles. Instead use:

  1. Environment variables
  2. Docker secrets
  3. External configuration files mounted at runtime

For Gmail specifically, generate an "App Password" instead of using your main account password.

If emails aren't arriving:

  • Check container logs for SMTP errors
  • Try sending to different email providers
  • Test with verbose SMTP logging: echo "Test" | mail -v -s "Test" user@domain.com
  • Verify your SMTP server isn't blocking the connection

When running monitoring scripts inside Docker containers, sending email notifications presents unique challenges compared to traditional servers. The container's isolated nature and minimal footprint require careful configuration of mail services.

The approach of installing Postfix directly in the container often fails because:

  • Containers typically lack proper hostname configuration
  • SMTP ports might be blocked in cloud environments
  • Postfix requires complex configuration for reliable delivery
  • Container IPs are often blacklisted by mail providers

Instead of running Postfix, consider these more container-friendly approaches:

1. Using External SMTP Relay

FROM ubuntu:latest
RUN apt-get update && apt-get install -y msmtp mailutils

COPY msmtprc /etc/msmtprc
RUN chmod 600 /etc/msmtprc

Example msmtprc configuration:

account default
host smtp.gmail.com
port 587
from your@gmail.com
auth on
user your@gmail.com
password your-app-password
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

2. Mailgun API Approach

#!/bin/bash
CURRENT=$(df /data | grep / | awk '{ print $5}' | sed 's/%//g')

if [ "$CURRENT" -gt "$THRESHOLD" ]; then
    curl -s --user 'api:YOUR_MAILGUN_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
    -F from='Alert ' \
    -F to=you@example.com \
    -F subject='Disk Space Alert' \
    -F text="Disk usage: $CURRENT%"
fi

For a lightweight solution, SSMTP works well in containers:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y ssmtp mailutils

COPY ssmtp.conf /etc/ssmtp/ssmtp.conf

Example ssmtp.conf:

root=postmaster
mailhub=smtp.gmail.com:587
AuthUser=your@gmail.com
AuthPass=your-app-password
UseTLS=YES
UseSTARTTLS=YES
hostname=yourhostname
FromLineOverride=YES

Always test your email setup during build:

RUN echo "Test email from container" | mail -s "Test" recipient@example.com
  • Never hardcode credentials in Dockerfiles
  • Use Docker secrets or environment variables
  • Consider dedicated email services for production
  • Implement proper logging for email failures

If emails aren't arriving:

  1. Check container logs for errors
  2. Verify network connectivity to SMTP server
  3. Test with simpler email content
  4. Try alternative ports (465 vs 587)
  5. Check spam folders

For mission-critical systems, consider:

version: '3'
services:
  monitor:
    image: your-monitor-image
    environment:
      SMTP_SERVER: smtp.relay.com
      SMTP_PORT: 587
      ALERT_EMAIL: admin@company.com