Send mail from Alpine docker container using Postfix on ubuntu host

Note Statistics

Note Statistics

  • Viewed 669 times
Thu, 08/27/2020 - 20:44

1 - Install Postfix

Ubuntu example. More detailed example here

sudo apt update
sudo DEBIAN_PRIORITY=low apt install postfix

2 Add your docker0 ip to postfix configuration

2.1 Find your docker0 ip

In your ubuntu host run

# ip addr show docker0

3 docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:a2:25:28:30 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:a2ff:fe25:2830/64 scope link
       valid_lft forever preferred_lft forever

In your container run

ip route show
default via 172.17.0.1 dev eth0 
172.17.0.0/16 dev eth0  src 172.17.0.4

This should show the same ip range as in the ubuntu host when running ip addr show docker0. If not then you should check the docker network configuration of your container.

2.2 Update postfix config

  1. Open the ssmtp config at ``
  2. Update mynetworks to you docker0 ip range (e.g 172.17.0.1/16) Dont forget to restart the postfix server (service postfix restart) every time you edit postfix conf file.
sudo systemctl restart postfix

2.3 Add docker0 to your iptables

You may need to modify the iptables rules on your host to allow connections from Docker containers. This will do the trick:

iptables -A INPUT -i docker0 -j ACCEPT

2.2 Alpine Container - Install ssmtp

BusyBox is the default Mail Transfer Agent. ssmtp is better since it allows adding configurations use these steps

Debugging

  • On unbuntu postfix logs are at /var/log/mail.log and /var/log/mail.err

Links

Notes

  • If you your host is running on EC2 instance you might get a Connection timed out message. This is due to AWS setting limits on outgoing request ( see)
  • You docker container must be in the default network. When using custom networks, make sure to add conatiners to default network also.
Authored by