Deploying mailcow with Docker: Building Your Own Mail Server Service (Part 1)

Publish: 2022-08-14 | Modify: 2022-08-16

In an earlier article, Xiaoz shared "Building a Mail Server with Docker, Self-hosted Mail Server" in which he introduced Poste, a lightweight and relatively simple-to-deploy mail server that is suitable for personal use. However, based on feedback from clients (whom Xiaoz has previously deployed Poste for), it seems that Poste is not very stable when sending a large number of emails and is prone to issues. Additionally, Poste puts all its related dependency services in a single container and does not use Docker Compose to orchestrate multiple containers, which compromises its stability.

Today, Xiaoz will share another open-source self-hosted mail server service called Mailcow. Mailcow offers more features than Poste and provides an official Docker Compose deployment method. Let's install the Mailcow mail service together with Xiaoz.

mailcow

Note: This tutorial is intended for individuals with a basic understanding of Linux and Docker.

Prerequisites

  • A VPS with a dedicated IP address, minimum configuration of 1C2G, recommended 2C4G or higher. The VPS should support outgoing mail service (no blocked email ports, please consult your service provider) and PTR reverse DNS lookup (consult your service provider).
  • Operating system: CentOS, Debian, or Ubuntu.
  • Docker and Docker Compose installed.
  • A domain name, preferably one that has been registered for a longer period of time, and it is recommended to use mainstream domain extensions such as .com/.net/.org.

Note: Please make sure to confirm with your service provider whether PTR reverse DNS lookup is supported. If it is not supported, emails are likely to end up in the spam folder. As far as Xiaoz knows, UltraVPS (EU) supports PTR reverse DNS lookup and allows you to perform the operation in the backend.

If you haven't installed Docker, you can refer to: Install Docker on Linux and Common Docker Commands.

If you haven't installed Docker Compose, you can use the following command to install it:

# Download Docker Compose
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Add executable permission
chmod +x /usr/local/bin/docker-compose
# Create a symbolic link
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Some CentOS 7 systems may have Postfix service installed by default, which may cause port conflicts with Mailcow. You can try uninstalling Postfix by executing the following command:

# Uninstall Postfix on CentOS
yum -y remove postfix
# Uninstall Postfix on Debian or Ubuntu
apt-get remove postfix

Set Linux Hostname

Assuming your top-level domain name is domain.com, your hostname needs to be set as mail.domain.com or mx.domain.com. You can set the hostname with the following command:

hostnamectl set-hostname mail.domain.com

Then modify /etc/hosts and add a hosts resolution line, which maps mail.domain.com to the public IP address of your current server. Use the following command:

echo '173.0.xx.xxx    mail.domain.com' >> /etc/hosts

Deploy Mailcow

First, clone the Mailcow code and scripts:

# Install Git on CentOS
yum -y install git
# Install Git on Debian
apt-get install git

# Clone the Mailcow code
git clone https://github.com/mailcow/mailcow-dockerized

Navigate to the code directory cd mailcow-dockerized and run the initialization script ./generate_config.sh to set the domain name, timezone, and other information as prompted. Note:

  1. The domain name should be the same as the hostname you set earlier, for example, mail.domain.com.
  2. If you need to make changes later, you can edit the mailcow.conf configuration file.

By default, Mailcow uses 80/443 as the web ports. If you already have a web service running on your server, it may cause port conflicts. You can modify the mailcow.conf configuration file to change the ports. For example, modify:

HTTP_PORT=80
HTTPS_PORT=443

to:

HTTP_PORT=880
HTTPS_PORT=8443

Then run the following commands to pull the container images and start the containers:

# Pull the images
docker-compose pull
# Start the containers
docker-compose up -d

Initialize Mailcow and Add Domains

After successful installation, access your IP + port to enter the web management interface. The default username is admin and the password is moohoo.

mailcow-login

After logging in, click "Configuration - Email settings - Add domain" in the upper right corner of the backend.

mailcow-add-domain

Note: When adding a domain, make sure to enter your top-level domain name domian.com. For example, if you set the hostname as mail.domain.com, enter domain.com when adding the domain.

Add Email DNS Records

After adding the domain, there is a DNS button on the right side. Clicking this button will check and guide you through the DNS configuration for your domain.

mailcow-dns

However, the detection results may not be accurate, and not all records need to be added. The following table lists the necessary records:

Hostname Record Type Record Value Note
mail.domain.com A Your IP Enter the public IP of your VPS
Your IP PTR mail.domain.com Contact your service provider to add this record if necessary
domain.com MX mail.domain.com
autodiscover.domain.com CNAME mail.domain.com
autoconfig.domain.com CNAME mail.domain.com
domain.com TXT v=spf1 a mx ip4:173.0.xx.xxx ~all Replace the IP with your own
dkim._domainkey.domain.com TXT Obtained from the Mailcow backend Obtain the DKIM record from the Mailcow backend

Test Email Delivery

After adding the DNS records, wait a few minutes for them to take effect. Then, add email accounts in the Mailcow backend and test sending emails using the webmail interface.

mailcow-webmail

Open https://www.mail-tester.com/ and send an email from your Mailcow mailbox. After sending, check the score on the Mail Tester website.

mail-tester

The maximum score is 10. If the score is low, Mail Tester will provide suggestions for improvement. It is recommended to follow the suggestions for better email deliverability.

Summary

The steps above only cover the installation and initialization. There are many additional configurations and maintenance tasks to be done. For example, disabling IPV6, reverse proxy, configuring SSL certificates, data backup and recovery, etc. If you are interested, you can refer to the Mailcow official documentation to complete these tasks. Xiaoz will share more about Mailcow configuration in the future.

Setting up a self-hosted mail server is complex and involves many steps. If not handled properly, emails may end up in the spam folder. Unless there is a specific need, it is recommended to use third-party enterprise email services. Although self-hosted mail servers are not subject to sending limits, it is not advisable to send a large number of spam emails in a short period of time, as this may result in your IP or domain being blacklisted, which is not worth the trouble.

If you need assistance with setting up a self-hosted mail server, you can contact me via QQ: 446199062 or WeChat: xiaozme.


Comments