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]
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.
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With