How to set up your own Docker mail server step by step

You can set up a dedicated mail server in just a few steps and thereby gain maximum control over your digital messaging traffic. Setup with Docker and its ready-to-use stack `Docker mail server´ is very popular. What are the requirements? And how exactly can a mail server be set up?

Tip

Set up your own professional email server in the blink of an eye. You benefit from large and expandable storage space, your own domain, automatic encryption as well as calendar and office functions or email archiving. IONOS also offers the highest security standards in ISO-certified data centers.

What hardware is needed for your Docker mail server?

You need the appropriate hardware to operate your own mail server. The dimensions will be determined by the performance required, such as how many messages are sent per day and how many people use the server at a time. The average size of the messages exchanged is also relevant. Further power should be planned for additional services such as a backup system or content scanner (protection against spam, phishing, etc.). You will also need a static IP address to ensure smooth interaction with other mail servers.

The capacities required will play a decisive role when choosing how to set up your mail server. A simple Start-Setup is sufficient for a handful of mails per day, while a heavily used mail server in the business sector should have enough RAM, CPU and storage space.

Note

Don’t forget to plan resources for the underlying operating system, whether you have Windows or Linux.

We have summarised three mail server scenarios and assigned a suitable IONOS server package for each.

Mail server scenario Hardware Suitable IONOS plan
Simple home server with little traffic 2 vCores, 4 GB RAM, 60 GB hard disk space VPS L
Mail server for a small company with up to 1,000 daily mails 4 vCores, 8 GB RAM, 100 GB hard disk space VPS XL
Enterprise mail server with over 50,000 daily mails 8 vCores, 16 GB RAM, 500 GB hard disk space AR6-32 SSD
Note

A dedicated server from IONOS gives you access to enterprise hardware which is reserved exclusively for you. In contrast, the `vServer´ and `Cloud Server´ models are based on virtualised resources which are shared with other customers — without performance losses.

Step by step tutorial: setting up a mail server with Docker

There are various options for setting up a mail server. In the following tutorial, we have selected a server based on the Docker container `Docker mail server´. The streamlined open source container (MIT license) provides a ready-to-use stack for setting up your own email server. All the important components are included, such as an SMTP server, IMAP or POP3 server, antispam, and antivirus software or an LDAP registry service.

Note

If you want to work with a Docker image like `Docker mail server´, you will need to install the container platform first. You can find out more on this in our extensive Docker tutorial.

Continue reading the following sections to learn exactly how the `Docker mail server´ setup works.

Step 1: Get Docker image

You can get the latest image of “Docker mail server” from the official stack register on DockerHub or from the official GitHub repository of the tool collection.

If you decide to use the DockerHub registry, get the image with the following command line input:

docker pull mailserver/docker-mailserver

Enter the following command to install it from the GitHub registry:

docker pull ghcr.io/docker-mailserver/docker-mailserver:edge

Step 2: Configure docker-compose.yml

The next step is to prepare the `Docker mail server´ container to start. For this, a registry for the mail server must be set up, such as `mail server Docker´. A new YAML file named docker-compose.yml should then be created in this registry and the following content for a basic configuration of the mail server can be added:

version: '3.8'
services:
    mailserver:
        image: docker.io/mailserver/docker-mailserver:latest
        container_name: mailserver
        hostname: mail-server
        # example.com should be replaced with the own domain for the mail server:
        domain name: example.com
        ports:
            - "25:25"
            - "587:587"
            - "465:465"
        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/
            # Path should be adjusted accordingly:
            - ./docker-data/nginx-proxy/certs/:/etc/letsencrypt/
            - /etc/localtime:/etc/localtime:ro
        environment:
            - ENABLE_FAIL2BAN=1
            # If letsencrypt for SSL/TLS certificate is used (otherwise adapt)
            - SSL_TYPE=letsencrypt
            - PERMIT_DOCKER=network
            - ONE_DIR=1
            - ENABLE_POSTGREY=0
            - ENABLE_CLAMAV=0
            - ENABLE_SPAMASSASSIN=0
            - SPOOF_PROTECTION=0
        cap_add:
            - NET_ADMIN # For Fail2Ban to work
            - SYS_PTRACE
Note

See the official manuals of Docker mail server on GitHub for a detailed list of possible settings for the mail server.

Step 3: Release ports

The three ports 25, 465, and 587 are entered in the YAML file. These are the classic SMTP server ports, which must also be released in the server’s firewall settings for smooth operation. The exact procedure depends on the system and provider — IONOS customers (vServer, Cloud Server, Dedicated Server) can conveniently complete the port release with the Cloud Panel. The required steps are as follows:

  1. Log in to the login page of the Cloud Panel with your personal customer data.
  2. Go to the `Server & Cloud´ section.
  3. Select the rented server hardware where you want to set up your mail server.
  4. Click `Network´ and then `Firewall Policies´ in the left side menu.
  5. Add the three TCP ports `25´, `465´, and `587´ one after the other using `Add Rule´.

Step 4: Apply host name to DNS settings

In the next step, make sure that the server’s DNS service uses an MX record for the hostname configured in Step 2 (here: `mail server´). Add an SPF TXT record with the hostname and the value `v=spf1 mx ~all´.

As an IONOS customer, you can also change your server’s DNS settings using the Cloud Panel as follows:

  1. Log in to the Cloud Panel login page if you are no longer logged in there.
  2. Click on the entry `Domains & SSL´.
  3. Click on the gear icon next to the domain entry where you set up your mail server and select `DNS´.
  4. You can now use the `Add Record´ button to add two entries for MX and SPF TXT.

Finally, press `Reset domain´ in the top menu to apply the changes. Confirm the adjustments you have made and click `Reset now´.

Tip

Do you want to store your electronic messages and have legal security and be GoBD compliant? Use the Email archiving from IONOS to archive selected inboxes legally and prevent any possible tampering!

Step 5: Generate DKIM keys

Create a DKIM entry to increase the mail server’s security level. This encryption technique can later be used to digitally sign messages. Use the setup.sh script file located in the Docker mail server registry to generate the key pair. Carry out the following command there:

./setup.sh config dkim

Add the generated entry — found in the file docker-data/dms/config/opendkim/keys/example.com/mail.txt — to the DNS records as another TXT entry so that receiving mail servers can retrieve the public key. The output looks like this:

mail._domainkey IN  TXT     ( "v=DKIM1; h=sha256; k=rsa; "
    "p=MIIBIjANBgkqhkiG9w0BAQEFACAQ8AMIIBCgKCAQEAaH5KuPYPSF3Ppkt466BDMAFGOA4mgqn4oPjZ5BbFlYA9l5jU3bgzRj3l6/Q1n5a9lQs5fNZ7A/HtY0aMvs3nGE4oi+LTejt1jblMhV/OfJyRCunQBIGp0s8G9kIUBzyKJpDayk2+KJSJt/lxL9Iiy0DE5hIv62ZPP6AaTdHBAsJosLFeAzuLFHQ6USyQRojefqFQtgYqWQ2JiZQ3"
    "iqq3bD/BVlwKRp5gH6TEYEmx8EBJUuDxrJhkWRUk2VDl1fqhVBy8A9O7Ah+85nMrlOHIFsTaYo9o6+cDJ6t1i6G1gu+bZD0d3/3bqGLPBQV9LyEL1Rona5V7TJBGg099NQkTz1IwIDAQAB" ) ; ----- DKIM key mail for example.com
Note

The encryption of mail communication with SSL/TLS is just as important as DKIM entries. Docker mail server developers recommend using letsencrypt for activation, but you can also use certificates which you have already purchased. Detailed setup instructions can be found in the mail server’s official GitHub documentation.

Step 6: Start server and create first address

Start the configured mail server directly from the registry with the following command:

docker-compose up

The first email address can be set up within two minutes. Use the following command for this, adjusting the domain, username, and password accordingly:

./setup.sh email add user@domain password

Additional profiles can be created at any time allowing you to set up your mail server exactly as you wish.

For the UK
Email hosting services
  • Personalised email address
  • Access from anywhere
  • Highest security standards
Was this article helpful?
Page top