Setup Mailhog inside Docker through Postfix (Ubuntu 18.04 LTS)
This is recommended for your dev and test servers.
Yesterday I had to set up Mailhog on one of our docker test servers, so we won't accidentally send emails to anyone. Plus we get a nice, simple web UI.
However, I ran into a problem I couldn't find an answer to anywhere; the
default_transport should be set to
/etc/postfix/main.cf, or else the mails won't be sent to Mailhog. This probably was because I installed postfix as "Local only".
So, I decided to make a complete tutorial.
Setup Mailhog inside Docker
Create a new folder and add this
version: '3' services: mailhog: image: mailhog/mailhog container_name: mailhog restart: always ports: - "1025:1025" # SMTP - "8025:8025" # Web UI
docker-compose up -d
See the web UI works at http://localhost:8025 (or http://[your-server-ip]:8025).
Postfix is needed so all mails are forwarded to Mailhog, including the host's
sudo apt install postfix
- Select "Internet Site"
- Use the default name and press Enter
relayhost = 127.0.0.1:1025 default_transport = smtp
Reload postfix for the changes to take effect:
sudo postfix reload
echo "Test mail" | mail -s "It works!" firstname.lastname@example.org
You should see the email in your web UI at http://localhost:8025
If you need to debug you can always
sudo tail -f /var/log/mail.log for the postfix log.
And for mailhog you can use
sudo docker-compose logs -f