Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure sendmail inside a docker container

I have a docker container running php and apache. The host is in an AWS instance which has the docker instance running. I am unable to send an email from the docker terminal. Is there any way to send an email from docker instance using sendmail which uses the docker's host's configuration?

The following command sends an email from host but doesn't send an email from docker instance. No error is given either.

echo "Subject: Testing Email" | cat - text | /usr/lib/sendmail -F [email protected] -t [email protected] 
like image 797
tarun mittal Avatar asked Oct 06 '14 11:10

tarun mittal


People also ask

Can you ssh into Docker?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.

How do I send an email using sendmail?

Once logged in, you can run the following command to send email: [server]$ /usr/sbin/sendmail [email protected] Subject: Test Send Mail Hello World control d (this key combination of control key and d will finish the email.)


1 Answers

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.

like image 110
xuhdev Avatar answered Sep 17 '22 13:09

xuhdev