Technical Tutorials
Set Up a Mail Server on a VPS (2025) - Docker Mailserver Containerized Practice
The ultimate guide to self-hosting a mail server in 2025. Use Docker Mailserver to quickly deploy a secure, modern mail service, with detailed explanations of DNS records (SPF/DKIM/DMARC/PTR) configuration, teaching you how to avoid sending blacklists and achieve a 10/10 email score.

In 2025, self-hosting a mail server is still the "ultimate dungeon" for tech enthusiasts. Although free services like Gmail and Outlook are convenient, owning your own domain email (such as [email protected]) not only looks professional but also gives you full control over your data privacy.
Traditional Postfix + Dovecot configuration is extremely tedious and error-prone. This article uses Docker Mailserver — an open-source containerized solution that integrates Postfix, Dovecot, SpamAssassin, ClamAV and other components — to get a secure, modern mail server running in 10 minutes.
Before starting, be sure to confirm the following three points, otherwise you will 100% fail:
- Port 25 is key: The vast majority of VPS providers (AWS, GCP, Vulter, etc.) block outbound traffic on port 25 by default. Without port 25, you cannot send mail to other mailboxes (such as Gmail).
- Self-test method:
telnet smtp.gmail.com 25. If it does not connect, submit a ticket to contact customer service to request unblocking, or switch to a provider that supports port 25 (such as Contabo, some Racknerd data centers).
- Self-test method:
- IP reputation: Check whether your VPS IP is on a blacklist (RBL). If the IP is dirty, sent mail goes straight to the spam folder.
- Reverse DNS (PTR): You must have permission to set PTR records. This is the first line of defense for major mailbox providers rejecting spam.
- Where to do it: This is not set in Cloudflare! Go to your VPS provider control panel (usually on the Networking or IPv4 Settings page), find "Reverse DNS" or "PTR Records", and set it to your mail domain (such as
mail.example.com).
- Where to do it: This is not set in Cloudflare! Go to your VPS provider control panel (usually on the Networking or IPv4 Settings page), find "Reverse DNS" or "PTR Records", and set it to your mail domain (such as
If the above conditions are met, Let's Go!
Before deploying the service, DNS must be configured first. Assume your IP is 1.2.3.4 and the domain is example.com.
Go to your domain provider (Cloudflare recommended) and add the following records:
| Type | Name | Content (Value) | Description |
|---|---|---|---|
| A | mail | 1.2.3.4 | Mail server address |
| MX | @ | mail.example.com | Set priority to 10 |
| TXT | @ | v=spf1 mx ~all | SPF record: declares that only the server in the MX record is allowed to send mail |
| A | example.com | 1.2.3.4 | (Optional) Main domain resolution |
There are two more important records (DKIM and DMARC) that will be configured after deployment.
Docker Mailserver needs to expose multiple ports. Please allow inbound traffic in the VPS firewall (such as UFW) or the cloud provider's security group:
- 25 (TCP): SMTP mail transfer (required)
- 80 & 443 (TCP): Used to apply for Let's Encrypt SSL certificates (required)
- 587 & 465 (TCP): Client mail sending (SMTP Submission)
- 993 (TCP): Client mail receiving (IMAP SSL)
# UFW 示例
ufw allow 25/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 587/tcp
ufw allow 465/tcp
ufw allow 993/tcp
-
Environment preparation: Make sure Docker and Docker Compose are installed. Note: If Nginx/Caddy is already running on your VPS (occupying port 80), the automatic certificate application in this tutorial will fail. It is recommended to deploy on a clean VPS.
-
Create directory and configuration:
mkdir -p mailserver/{config,data} && cd mailserver -
Get the official configuration file (recommended):
wget -O compose.yaml https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master/compose.yaml wget -O .env https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master/mailserver.env -
Modify the
.envfile:vim .env, focusing on modifying the following items:HOSTNAME=mail.example.com # 你的邮件服务器完整域名 DOMAINNAME=example.com # 你的主域名 SSL_TYPE=letsencrypt # 自动申请 SSL 证书 -
Start the service:
docker compose up -d
Docker Mailserver provides a setup.sh script to manage the service.
-
Download the management script:
wget -O setup.sh https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master/setup.sh chmod +x setup.sh -
Create a mailbox account:
./setup.sh email add [email protected] strongPassword123 -
Generate DKIM keys (key to anti-spoofing):
./setup.sh config dkimThe generated public key is located at
docker-data/dms/config/opendkim/keys/example.com/mail.txt. -
Go to Cloudflare to add the DKIM record: View the contents of
mail.txtand extract the long string afterp=.- Type: TXT
- Name:
mail._domainkey - Content:
v=DKIM1; k=rsa; p=你的公钥字符串
-
Add a DMARC record (improve reputation):
- Type: TXT
- Name:
_dmarc - Content:
v=DMARC1; p=none; rua=mailto:[email protected]
A few minutes after the service starts, it will automatically apply for an SSL certificate through Let's Encrypt.
Client connection parameters:
- Email address:
[email protected] - IMAP (receiving):
mail.example.com, port 993 (SSL/TLS) - SMTP (sending):
mail.example.com, port 465 (SSL/TLS) or 587 (STARTTLS)
Enter the above information in Outlook / Apple Mail on your phone or computer to send and receive mail.
After configuration is complete, do not rush to send bulk mail.
- Test score: Visit Mail-Tester, get a temporary mailbox, and send an email from your server to it. If you strictly follow the above steps (especially SPF/DKIM/PTR), you should get a perfect 10/10 score.
- Data backup (important): All mail data and configuration are stored in the
mailserver/docker-datadirectory. Be sure to back up this directory regularly! Once lost, all mail cannot be recovered. (Reference: VPS automatic backup solution)
Self-hosting email is a cultivation journey; may your mail never land in the spam folder!