How to Self-Host Email with Docker Mailserver

Tired of handing your private emails over to Google, Microsoft, or some faceless third party? You’re not alone. If it feels like your inbox isn’t really yours anymore, it’s time to take it back. And the best way to do that? Self-host your email.

Self-hosting your own email might sound like a job for tech wizards, but with Docker Mailserver, it’s way more doable than you’d think.

In this guide, you’ll learn how to run a secure, private email server right on your own VPS or home server using Docker. No complex control panels. No mysterious black boxes. Just full control, straight from the terminal. Ready to take charge of your digital life? Let’s get started!

Why Choose Self-Hosted Email: Privacy, Control, and Customization

Self-hosting email is great for data privacy and autonomy. It’s also great for control and customisation over the software, storage, and user control. While it is a complex endeavour, solutions like Docker Mail Server simplify deployment and accessibility for those who are interested.

What is docker-mailserver (DMS)?

Docker Mailserver is a popular open-source mail server solution that is deployed as Docker containers. Designed to minimize the complexity of email hosting, it now requires only simple Docker Compose and configurations.

DMS includes everything essential to an email server deployment, including Postfix, Dovecot, various anti-spam tools like SpamAssassin, and anti-virus (ClamAV) protection.

Due to its simple and all-in-one design, the requirement for a self-hosting email server is quite low and doesn’t require meticulous configurations like in the past.

What You’ll Achieve with This Tutorial

In this tutorial, we will set up the mail.flywp.me email server, which will handle all email for the domain flywp.me. It should look something like this:

How to Self-Host Email with Docker Mailserver

This tutorial aims to guide users through the entire process of establishing a functional and secure self-hosted email server using Docker Mailserver. It will cover everything from initial server preparation and Docker installation to configuring DMS, setting up crucial DNS records, managing users, implementing security measures, and testing the final setup.

While Docker Mailserver greatly simplifies many aspects of mail server deployment, it’s important to recognize that self-hosting email is a significant undertaking that demands careful attention to detail, ongoing maintenance, and a commitment to understanding the underlying technologies.

Laying the Groundwork: Prerequisites for Self-Hosted Email

Before embarking on the journey of self-hosting email, it’s crucial to understand the commitments involved and ensure the necessary resources and knowledge are in place. This section outlines the pros and cons, essential toolkit, and technical familiarity required.

Before we start, we need to establish some ground rules on what we need. We will outline the pros and cons, essential toolkits, and the required technical familiarity.

Essential Toolkit: Domain Name, Static IP (or VPS), and System Resources

Essential tools for hosting email with docker mailserver

A few key components are indispensable for a self-hosted email setup:

  • Domain Name
  • Static IP Address with Good Reputation
  • VPS (Virtual Private Server) with the following ports allowed:
    • 25/tcp (SMTP)
    • 143/tcp (IMAP)
    • 465/tcp (SMTPS)
    • 587/tcp (SMTP Submission)
    • 993/tcp (IMAPS)

In this tutorial, we will be using an Ubuntu 24.04 server.

Weighing the Pros and Cons: Is Self-Hosting Right for You?

While self-hosting email is great for a lot of people, there are some considerable downsides to it. Which is why many in the self-hosting communities advise against self-hosting email. However, there are still certain niches for self-hosted emails. Many also do this for learning how email works.

Pros:

  • Data Privacy & Ownership: Maintaining Ownership of email data in the age of data mining and AI training on any data they can access.
  • Storage Capacity: Self-hosting email helps to avoid high markups on email storage offered by some providers.
  • Hosting Others: The option to provide email accounts for friends, family, or a small business.

Cons:

  • Technical Complexity: The technical complexity required for running a mail server can be quite demanding in certain areas. While the basics are simple, complex setups with anti-spam tools and solving email deliverability can be quite hard.
  • Unreliable Availability: Traditional email providers have an upper hand in reliability due to their infrastructure and experience, which can be hard to achieve for simple users.

The “technical complexity” aspect extends far beyond the initial setup. The administrator assumes the roles of system administrator, security officer, and deliverability specialist, responsible for all ongoing operations. This continuous management responsibility is a direct trade-off for the control and privacy gained.

Technical Know-How: Basic Linux and Docker Familiarity

We assume that you have basic Linux knowledge on how to edit configuration files on a remote server.

Step 1: How to Set Up Your Docker Environment

Installing Docker Engine:

Docker provides an easy-to-use script for installing Docker on Linux servers. Simply run the script below with sudo permission, and Docker will be installed.

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Deploying and Configuring Docker Mail Server

With Docker installed, the next phase is to deploy and configure the docker-mailserver itself. This will require us to obtain the necessary config files, then customize them, and perform the initial launch.

Setting Up and Configuring Docker Mailserver

The docker-mailserver project provides example configuration files that serve as an excellent starting point. It is highly recommended to fetch the official compose.yaml (for Docker Compose) and mailserver.env (for environment variables) files directly from the project’s GitHub repository.

First, create a directory for your mail server configuration, for example, ~/mailserver, and navigate into it:

mkdir ~/mailserver
cd ~/mailserver

Then, download the files. The URL typically points to the master branch for the latest stable files:

DMS_GITHUB_URL="https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master"
wget "${DMS_GITHUB_URL}/compose.yaml"
wget "${DMS_GITHUB_URL}/mailserver.env"

Alternatively, for users who wish to explore examples, contribute, or work with a specific version, cloning the entire docker-mailserver GitHub repository is an option. However, for basic deployment, the two files above are sufficient.

Step 2: How to Configure compose.yaml and mailserver.env

These two files are central to configuring and running docker-mailserver.

compose.yaml:

This YAML file defines the docker-mailserver service for Docker Compose. It specifies the Docker image to use, container name, hostname, environment file, port mappings, and volume mounts.

This is what the default compose.yaml for docker-mailserver looks like (customisations will be needed):

services:

  mailserver:

    image: ghcr.io/docker-mailserver/docker-mailserver:latest
    container_name: mailserver

    # Provide the FQDN of your mail server here (Your DNS MX record should point to this value)
    hostname: mail.example.com
    env_file: mailserver.env
    # More information about the mail-server ports:
    # https://docker-mailserver.github.io/docker-mailserver/latest/config/security/understanding-the-ports/

    ports:
      - "25:25"    # SMTP  (explicit TLS => STARTTLS, Authentication is DISABLED => use port 465/587 instead)
      - "143:143"  # IMAP4 (explicit TLS => STARTTLS)
      - "465:465"  # ESMTP (implicit TLS)
      - "587:587"  # ESMTP (explicit TLS => STARTTLS)
      - "993:993"  # IMAP4 (implicit TLS)

    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

    restart: always
    stop_grace_period: 1m
    # Uncomment if using `ENABLE_FAIL2BAN=1`:
    # cap_add:
    #   - NET_ADMIN

    healthcheck:
      test: "ss --listening --ipv4 --tcp | grep --silent ':smtp' || exit 1"
      timeout: 3s
      retries: 0

If you look closely at the Docker Compose file, you will see almost everything is configured here except for the domain name at line number 6. There are other options here, but for now, we will only be modifying the domain section in this Docker Compose file. We will use the following domain mail.flywp.me for email service. So, we will replace `mail.example.com` with mail.flywp.me.

However, note that hostname is a critical setting. It must be set to the Fully Qualified Domain Name (FQDN) of the mail server, for example, mail.yourdomain.com. This hostname is used internally by Postfix and other services and must align with DNS records for proper operation.

Mailserver.env:

This file contains configuration settings for our email server. For now, we do not need to modify anything in this file.

Step 3: Initial Launch and Basic Configuration

Once compose.yaml and mailserver.env are configured, the mail server can be launched with Docker Compose:

docker compose up -d

On first start, we will need to add at least one email account. We have two minutes to do so; otherwise, DMS will shut down and restart. 

[NOTE: We must run this command within 120 seconds of the container start]

You can add accounts by running, replacing with your domain as appropriate:

docker exec -ti mailserver setup email add [email protected]
Initial Launch and Basic Configuration of Docker Mailserver

That’s it! It is that easy.

Step 4: DNS and Domain Configuration

Now begins the Fun parts, where we will be configuring our DNS settings, so that we can tell the internet where to send our email to.

Why do we need this? DNS configuration is there to ensure that the rest of the internet knows where we are located and forwards our email to the correct destination. For this, we will need to set up some DNS records.

Now let’s say you just bought example.com and you want to be able to send and receive e-mails for the address [email protected]. On the most basic level, you will need to

Set an MX record for your domain example.com – in our example, the MX record contains mail.flywp.me.

DNS and Domain Configuration

Set an A record that resolves the name of your mail server – in our example, the A record contains 11.22.33.44

DNS and Domain Configuration

And optionally, set a PTR record that resolves the IP of your mail server – in our example, the PTR contains mail.flywp.me. We will take a look at DKIM, DMARC & SPF later, but for now, these are the records that suffice in getting you up and running. You might be wondering what each of these records does. Here is a brief explanation.

  • MX Record: Tells other servers where to send emails for your domain (e.g., yourdomain.com). It points to your mail server’s name (e.g., mail.yourdomain.com). This doesn’t change your email address; it remains [email protected]. A single mail server can handle emails for multiple domains if their MX records point to it.
  • A Record: Links a hostname (like mail.yourdomain.com) to its specific IP address.
  • PTR Record: Does the opposite of an A record. It links an IP address back to a hostname. This is often used for reverse DNS lookups.

Here is a simple table explaining it.

Record TypeHost/NameValue/TargetPurposedocker-mailserver Considerations
Amail.yourdomain.comyour.server.ipv4.addressMaps hostname to IPv4 address.Must match the public IPv4 of your server running DMS. Aligns with hostname in compose.yaml.
AAAA (Optional)mail.yourdomain.comyour:server:ipv6:addressMaps hostname to IPv6 address (if used).Must match the public IPv6 of your server running DMS. Aligns with hostname in compose.yaml.
MXyourdomain.com10 mail.yourdomain.comDirects incoming email for yourdomain.com to mail.yourdomain.com.Points to the hostname defined in A/AAAA records (e.g., mail.yourdomain.com).

Now, if everything is set up correctly, we should be able to verify it pretty easily. We can use a tool on our Linux server to verify these configurations. You can replace the domain name with your own.

$ nslookup -type=A mail.flywp.me 1.1.1.1

Server:         1.1.1.1
Address:        1.1.1.1#53

Non-authoritative answer:

Name:   mail.flywp.me
Address: 152.42.202.111

$ nslookup -type=MX flywp.me 1.1.1.1
Server:         1.1.1.1
Address:        1.1.1.1#53

Non-authoritative answer:
flywp.me        mail exchanger = 10 mail.flywp.me.

On our DNS portal end, everything looks okay.

DNS and Domain Configuration

Step 5: Accessing and Using the Email Server

Now that we have set up our email server, it is time to use it. We will be using Thunderbird for testing our email server by checking if we can receive any email from other providers, such as Google.
To add our email to Thunderbird, just click on Add a new account in Thunderbird.

Accessing and Using the Email Server


And Thunderbird can autodetect settings for our mail server. Just click on Done, and you are good to go. Now that we have added our email to Thunderbird, let’s try receiving an email from a Gmail account.

Accessing and Using the Email Server

And let’s send it and check our Thunderbird app for the email. And voila, we can see our test email in our inbox.

Accessing and Using the Email Server

How FlyWP Makes Email Sending (and Hosting) Smarter

Built specifically with developers in mind, FlyWP doesn’t just host your WordPress sites. It handles the technical bits you shouldn’t have to babysit – like transactional email. No third-party SMTP plugin. No wrestling with wp-config or PHP settings. Just a few clicks, and your site is ready to send mail through providers like Postmark, SendGrid, Mailgun, or even your custom SMTP.

And because FlyWP integrates directly with your site, you get faster email performance, better error reporting, and fewer reasons to dig around in logs wondering where your message went. Learn more about FlyWP’s WordPress email sending feature.

Wrapping Up

Self-hosting your email with Docker Mailserver isn’t just a tech flex; it’s a step toward true digital independence. You get control, privacy, and flexibility, all without relying on big providers to guard your inbox. Here’s a quick recap of what you’ve tackled throughout this article:

  • Preparation – You set up your domain, server, and got Docker ready.
  • Docker Mailserver Deployment – You spun up the container, created users, and configured the basics.
  • DNS Configuration – You handled the core records like MX, A, and PTR, plus essentials like SPF, DKIM, and DMARC.
  • Testing and Maintenance – You checked your setup, tested email flow, and configured email clients.

That’s no small feat. You now have a working, private email system running on your own terms. Want to go further? Look into adding webmail, automated backups, or integrating with other self-hosted tools. Your inbox is now truly yours. Own it.

Add your first comment to this post