大网数据广告活动
服务器运维问答

Simple way to send mail from Docker container?

· 52CE 我爱测

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…

问题原文

原帖链接 · Olivier Lalonde · CC BY-SA 3.0

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 omitted] << 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 omitted]
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.

社区高分解答

原帖链接 · Rick · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。