Docker Mailserver
A production-ready, full-stack mail server solution that includes SMTP, IMAP, anti-spam, antivirus and more, running in a container
Alternatives
Tech stack
No tech stack added yet. Sign in to suggest some
Docker Mailserver is a production-ready, full-stack mail server solution designed to simplify the deployment and management of mail servers. It combines Postfix, Dovecot, SpamAssassin, ClamAV and other components in a single Docker container, providing a complete set of mail services.
Core Features
Mail Service Features
- SMTP server: Reliable mail transfer based on Postfix
- IMAP/POP3 server: Mail access service based on Dovecot
- Secure transport: Supports TLS/SSL encrypted connections
- Mail authentication: Supports SASL authentication mechanisms
- Virtual users: Supports virtual domains and user management
- Mail forwarding: Flexible mail forwarding and alias configuration
Security Protection
- Anti-spam: Integrates SpamAssassin to filter spam
- Antivirus scanning: Integrates the ClamAV virus scanning engine
- SPF/DKIM/DMARC: Complete email authentication support
- Fail2ban: Automatically bans malicious IP addresses
- Greylisting: Postgrey greylisting mechanism
- Rate limiting: Prevents the mail server from being abused
Management Features
- Configuration-file driven: Only configuration files are needed, no SQL database required
- Docker native: Designed specifically for containerized environments
- Automation scripts: Provides convenient management scripts
- Log management: Detailed logging and monitoring
- Backup friendly: Simple data backup and recovery
- Multi-domain support: Supports managing multiple mail domains
Technical Architecture
Core Components
- Postfix: SMTP mail transfer agent
- Dovecot: IMAP/POP3 mail retrieval agent
- SpamAssassin: Spam filtering engine
- ClamAV: Open-source antivirus engine
- Amavis: Mail content filtering framework
- Fail2ban: Intrusion prevention system
- Postgrey: Greylisting implementation
- OpenDKIM: DKIM signing and verification
Supported Protocols
- SMTP/SMTPS: Mail sending protocols
- IMAP/IMAPS: Mail access protocols
- POP3/POP3S: Mail download protocols
- ManageSieve: Mail filtering rule management
Deployment Requirements
System Requirements
- Operating system: A Linux distribution that supports Docker
- Memory: At least 1GB RAM (2GB+ recommended)
- Storage: 10GB+ of free space
- Network: A public IP address and a domain name
- Ports: 25, 143, 465, 587, 993, 995
Network Configuration
- DNS records: Configure MX, A and AAAA records
- SPF record: Configure the Sender Policy Framework
- DKIM record: Configure DomainKeys Identified Mail
- DMARC record: Configure Domain-based Message Authentication, Reporting and Conformance
- Reverse DNS: Configure a PTR record
Docker Compose Deployment Example
version: '3.8'
services:
mailserver:
image: ghcr.io/docker-mailserver/docker-mailserver:latest
container_name: mailserver
hostname: mail.example.com
env_file: mailserver.env
volumes:
- ./docker-data/dms/mail-data/:/var/mail/
- ./docker-data/dms/mail-state/:/var/mail-state/
- ./docker-data/dms/mail-logs/:/var/log/mail/
- ./docker-data/dms/config/:/tmp/docker-mailserver/
- /etc/localtime:/etc/localtime:ro
ports:
- "25:25" # SMTP (explicit TLS => STARTTLS)
- "143:143" # IMAP4 (explicit TLS => STARTTLS)
- "465:465" # ESMTP (implicit TLS)
- "587:587" # ESMTP (explicit TLS => STARTTLS)
- "993:993" # IMAP4 (implicit TLS)
restart: always
stop_grace_period: 1m
cap_add:
- NET_ADMIN
healthcheck:
test: "ss -lntp state listening | grep -P ':(25|143|465|587|993)\\b'"
timeout: 3s
retries: 0
Environment File Example (mailserver.env)
# 基本配置
OVERRIDE_HOSTNAME=mail.example.com
[email protected]
# SSL/TLS 配置
SSL_TYPE=manual
SSL_CERT_PATH=/var/mail-state/manual-ssl/cert
SSL_KEY_PATH=/var/mail-state/manual-ssl/key
# 安全特性
SPOOF_PROTECTION=1
ENABLE_SPAMASSASSIN=1
ENABLE_CLAMAV=1
ENABLE_FAIL2BAN=1
ENABLE_POSTGREY=1
# DKIM 配置
ENABLE_OPENDKIM=1
OPENDKIM_KEY_LENGTH=2048
# 用户认证
ENABLE_SASLAUTHD=0
SASLAUTHD_MECHANISMS=rimap
SASLAUTHD_MECH_OPTIONS=127.0.0.1
# 其他设置
ONE_DIR=1
DMS_DEBUG=0
User Management
Adding User Accounts
# 使用管理脚本添加用户
./setup.sh email add [email protected] password123
# 添加邮件别名
./setup.sh alias add [email protected] [email protected]
# 列出所有用户
./setup.sh email list
Domain Configuration
# 添加新域名
echo "example.com" >> docker-data/dms/config/postfix-virtual-mailbox-domains
# 配置域名邮箱
echo "[email protected] example.com/user/" >> docker-data/dms/config/postfix-virtual-mailbox-maps
Security Configuration
SSL/TLS Certificates
- Let's Encrypt: Use Certbot to request and renew certificates automatically
- Self-signed certificates: For test environments
- Commercial certificates: For production environments
Firewall Configuration
# 开放必要端口
ufw allow 25/tcp # SMTP
ufw allow 143/tcp # IMAP
ufw allow 465/tcp # SMTPS
ufw allow 587/tcp # SMTP Submission
ufw allow 993/tcp # IMAPS
DNS Security Records
; SPF Record
example.com. IN TXT "v=spf1 mx ~all"
; DMARC Record
_dmarc.example.com. IN TXT "v=DMARC1; p=quarantine; rua=mailto:[email protected]"
; DKIM Record
mail._domainkey.example.com. IN TXT "v=DKIM1; h=sha256; k=rsa; p=PUBLIC_KEY_HERE"
Monitoring and Maintenance
Log Monitoring
- Mail log:
/var/log/mail/mail.log - Authentication log:
/var/log/mail/mail.info - Error log:
/var/log/mail/mail.err - Spam log: Detailed SpamAssassin analysis logs
Health Checks
# 检查服务状态
docker exec mailserver supervisorctl status
# 测试邮件发送
echo "Test mail" | mail -s "Test Subject" [email protected]
# 检查队列状态
docker exec mailserver postqueue -p
Backup Strategy
# 备份邮件数据
tar -czf mailserver-backup.tar.gz docker-data/dms/
# 备份配置文件
cp -r docker-data/dms/config/ config-backup/
Troubleshooting Common Issues
Mail Is Being Rejected
- Check your DNS record configuration
- Verify your SPF/DKIM/DMARC settings
- Confirm that your IP address is not on a blacklist
- Check reverse DNS resolution
Performance Optimization
- Adjust the number of concurrent Postfix connections
- Optimize ClamAV memory usage
- Configure an appropriate disk I/O scheduler
- Enable mail compression
Use Cases
- Small businesses: Internal company mail systems
- Individual users: Private mail servers
- Development and testing: Mail testing environments for applications
- Educational institutions: School mail systems
- Hosting services: Mail hosting service providers
Advantages
Compared with Traditional Mail Servers
- ✅ Simplified configuration, with no complex database setup
- ✅ Containerized deployment, easy to migrate and scale
- ✅ Complete built-in security protection
- ✅ Active community support and documentation
Compared with Cloud Mail Services
- ✅ Full control over your data and privacy
- ✅ No monthly subscription fees
- ✅ Custom configuration and extensible features
- ✅ Not restricted by third-party services
Docker Mailserver gives users who need self-hosted mail a powerful, secure and easy-to-manage solution. Its complete feature set and containerized architecture make it an ideal choice for modern mail server deployments.
Discover more
AnonAddy
Email forwarding service for creating aliases.
AnyCable
Realtime server for reliable two-way communication over WebSockets, Server-sent events, etc.
Apprise
Apprise allows you to send a notification to almost all of the most popular notification services available to us today such as: Telegram, Discord, Slack, Amazon SNS, Gotify, etc.
Artalk
Comment system built in Golang, providing a lightweight and highly customizable solution for adding comments to your website.
Centrifugo
Language-agnostic real-time messaging (Websocket or SockJS) server.
Eqivo
Eqivo implements an API layer on top of FreeSWITCH facilitating integration between web applications and voice/video-enabled endpoints such as traditional phone lines (PSTN), VoIP phones, webRTC clients etc.