Configure sendmail inside a docker container
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.
building on previous answers,
create config/sendmail_config.sh with:
#!/bin/sh# set host in hostsline=$(head -n 1 /etc/hosts)line2=$(echo $line | awk '{print $2}')echo "$line $line2.localdomain" >> /etc/hostsyum install -y sendmail sendmail-cf m4 \ && hostname >> /etc/mail/relay-domains \ && m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf#remove localhost limitsed -i -e "s/Port=smtp,Addr=127.0.0.1, Name=MTA/Port=smtp, Name=MTA/g" \ /etc/mail/sendmail.mcsendmail -bd
change yum for apt-get on debian based containers
then in Dockerfile add:
RUN sed -i -e "s#;sendmail_path =#sendmail_path = /usr/sbin/sendmail -t -i#g" \ /your_path_to/php.iniCOPY ./config/sendmail_config.sh .
I want sendmail with my php util so I can stick it anywhere with out having to link to another MTA container or host to complete the task.
I run sh sendmail_config.sh, and then run my php util.