A Docker Compose configuration for running MailHog - an email testing tool for developers that captures outgoing emails and displays them in a web interface.
MailHog is a email testing tool that acts as a fake SMTP server. It captures any emails sent to it and displays them in a web UI, making it perfect for:
1025: SMTP server (for sending emails)80: Web UI (for viewing captured emails)Start MailHog:
docker-compose up -d
Check service status:
docker-compose ps
Access the Web UI: Open your browser and navigate to http://localhost
View logs:
docker-compose logs -f mailhog
Point your application's email configuration to use MailHog's SMTP server:
SMTP Settings:
localhost (or mailhog if connecting from another Docker container)1025Python (using smtplib):
import smtplib
from email.mime.text import MIMEText
msg = MIMEText("This is a test email")
msg['Subject'] = 'Test Email'
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'
with smtplib.SMTP('localhost', 1025) as server:
server.send_message(msg)
Node.js (using nodemailer):
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'localhost',
port: 1025,
secure: false,
ignoreTLS: true
});
transporter.sendMail({
from: 'sender@example.com',
to: 'recipient@example.com',
subject: 'Test Email',
text: 'This is a test email'
});
PHP:
ini_set('SMTP', 'localhost');
ini_set('smtp_port', 1025);
mail('recipient@example.com', 'Test Email', 'This is a test email');
Environment Variables (common pattern):
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Stop MailHog:
docker-compose down
Restart MailHog:
docker-compose restart
View real-time logs:
docker-compose logs -f mailhog
If your application runs in another Docker container, add it to the same network as MailHog:
services:
your-app:
# ... your app config
depends_on:
- mailhog
environment:
MAIL_HOST: mailhog
MAIL_PORT: 1025
mailhog:
# ... mailhog config from above
Then use mailhog:1025 as your SMTP server instead of localhost:1025.
"8025:8025" and update the web UI access to http://localhost:8025localhost:1025 (or mailhog:1025 from Docker)docker-compose ps"8025:8025" in the ports sectionservices:
mailhog:
image: mailhog/mailhog
container_name: 'mailhog'
restart: unless-stopped
ports:
- "1025:1025"
- "80:8025"