What I do is to configure the host MTA to listen on docker0
and install ssmtp in the container to bridge sendmail in the container with the host MTA. The reason to run an MTA on the host is that system (critical) errors can be sent to the admin's mailbox. The reason to not run MTA in the container is that it is a duplicated process as the host system already runs an MTA.
On the host, I used postfix. All we need to do is to configure postfix to listen on docker0
and accept outgoing mails from Docker containers. Edit the file /etc/postfix/main.cf
and add the docker0
IP address to inet_interfaces
so it accepts connections from Docker containers. Also, add the network addresses of Docker containers to mynetworks
so that Docker containers are legal to send mails via the postfix server on the host. (reference and more details)
To use sendmail in containers, install ssmtp and set FromLineOverride
to be permitted and and mailhub
to the IP address of the host in /etc/ssmtp/ssmtp.conf
. You can set mailhub
to be a symbol such as smtp-server
and then run the container with --add-host
option, as shown in this Dockerfile (run it with --add-host smtp-server:your-docker0-address
). This would configure a usable sendmail in containers which would actually use the host MTA to send mails.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…