Step-by-Step Guide to Installing Mailcow with Docker
Switch from Gmail and set up Mailcow with Docker to host your own email server.
Prerequisites
- Debian/Ubuntu VPS from a hosting provider that allows port 25. If port 25 is blocked, you can receive but not send emails.
Docker Installation
Download Docker:
sudo apt-get install -y ca-certificates curl && sudo mkdir -p /etc/apt/keyrings && sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc && sudo chmod a+r /etc/apt/keyrings/docker.asc && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null && sudo apt-get update
Install Docker and Docker Compose:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
(Retrieved from Docker documentation on August 25, 2024)
Add yourself to the Docker group to allow you running Docker commands without needing sudo every time. Replace username with your own:
sudo usermod -aG docker username
Start Docker on boot:
sudo systemctl enable docker
Mailcow
Download Mailcow:
mkdir ~/docker; cd ~/docker
git clone https://github.com/mailcow/mailcow-dockerized; cd mailcow-dockerized
./generate_config.sh
When prompted, enter your mail server hostname (e.g., mail.example.com
) and your timezone (e.g., US/Pacific
).
Edit configuration file to reduce RAM usage.
nano mailcow.conf
Find and set the following lines:
SKIP_CLAMD=y
SKIP_SOGO=y
SKIP_SOLR=y
Start Mailcow:
docker compose up -d
Go to http://mail.example.com and log in with the username admin
and password moohoo
. Change your password in System > Configuration.
Go to E-Mail > Configuration and add a new domain.
On the same page, click DNS to view DNS records. Update these records with your hosting provider or domain registrar.
Go to Mailboxes tab and create a new mailbox.
Reverse DNS
Set reverse DNS (PTR record) to mail.example.com
for both IPv4 and IPv6 at your hosting provider. For examples, see Hetzner Cloud or Linode.
Thunderbird
Open Thunderbird and connect to your mailbox.
IMAP (receiving emails)
Hostname: mail.example.com
Port: 993
Connection security: SSL/TLS
SMTP (sending emails)
Hostname: mail.example.com
Port: 465
Connection security: SSL/TLS
Create an OpenPGP key in Thunderbird’s settings to send encrypted emails.
Reverse Proxy
Ports 80 and 443 are often used by web servers. Change Mailcow’s ports to avoid conflicts with other servers on your VPS.
nano mailcow.conf
Change HTTP_PORT=80
and HTTPS_PORT=443
:
HTTP_PORT=8081
HTTPS_PORT=8443
Restart Mailcow:
docker compose down; docker compose up -d
Install Caddy:
sudo apt install caddy
Open Caddyfile:
nano /etc/caddy/Caddyfile
Update Caddyfile:
example.com {
route /mail* {
uri strip_prefix /mail
redir https://mail.{host}{uri}
}
}
mail.example.com {
reverse_proxy localhost:8081
}
Restart Caddy:
sudo systemctl restart caddy
Go to Mailcow admin dashboard at https://mail.example.com or at https://example.com/mail if you prefer using a subpath.
However, it’s not safe to leave the admin dashboard available on the internet all the time.
To disable access to the admin dashboard, add responnd 403
to your Caddyfile:
mail example.com {
reverse_proxy localhost:8081
respond 403
}
To enable access to the admin dashboard, comment out respond 403
in your Caddyfile:
mail example.com {
reverse_proxy localhost:8081
#respond 403
}