I have a backup script that runs inside a Docker container and I would like it to send me an email when disk usage gets low.
Here's the script:
#!/bin/bash
CURRENT=$(df /data | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90
echo "$CURRENT"
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
echo "Low Disk Space Alert: ${CURRENT}% used"
mail -s 'Disk Space Alert' [email protected] << EOF
Backup server remaining free space is critically low. Used: $CURRENT%
EOF
fi
The problem is that I can't manage to get mail to work inside the container. Here's my Docker file:
FROM ubuntu:latest
MAINTAINER [email protected]
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y postfix && \
apt-get install -y mailutils && \
apt-get clean
ADD rootfs /
CMD /disk-alert.sh
I've tried a few things but the best I could do is get the mail command to complete without error (though I didn't receive anything to my email).
Is there a simple way to configure my container to be able to send mail? I don't care if the mail goes to spam as long as I receive it.
As Jason already mentioned, I think it is indeed cleaner to separate your MTA and backup container.
I ran into this problem some time ago (although I tried to send mail from PHP instead of bash), and I discovered the exim-sender container, which is basically a simple container that acts as an outgoing SMTP server.
Why not link your backup container to such an SMTP container? If you then use ssmtp, you can send mails through the SMTP container. Or you can just use another (external) SMTP server if you prefer.
Consider sending an email from something like Amazon SES or MailGun or any other HTTP-based mail API rather than adding an entire MTA to your container.
Or make an API call to a alerting system such as OpsGenie or PagerDuty for it.