Install a mail server on Linux in 10 minutes – with docker, docker-compose, and mailcow

Did you ever want to have a complete, easy, and fast tutorial on how to install and set up a mail server on Linux that can be done in 10 minutes? You don’t want to rely on expensive all-in-one solutions but instead, use only proven open-source applications?

Here you will learn step-by-step how to install and set up all necessary applications to have a fully-featured mail server. And trust me, you can do it in about 10 minutes!

Prerequisites:

You can use this tutorial once you have an installed Linux server. In my example, I used the distribution Ubuntu 18.04 LTS, but you can use this tutorial on almost every Linux distribution that supports docker. Note, that the installation of docker can be slightly different.


1. Why is a mail server set up more complex?

Mail servers are more complex than most other applications to install and set up on Linux. This is because there is no single “mail server” application, as a mail server is a bunch of several applications and services. This is based on what you expect from a fully-featured mail server and what open-source applications can offer. You probably need an MTA (mail transfer agent) which is responsible for mail transport. But you may also need the ability to collect your emails via IMAP and POP3, a webmail application, an antispam solution or a database.

Of course, there is an open-source solution for all of these tasks. They are well proven because they are existing since the earliest beginning of the internet. But there are several options for services and applications you could use and they all need to interact with each other. The configuration is all done via static config files for each service. Therefore, it can be very complex to set up a fully-featured mail server on Linux.

But don’t worry! There are also solutions to make it more easy, smooth and seamless.


2. How can we simplify this approach?

There are three main components we will use to simplify the installation, configuration, and management of these different applications.

docker

Docker: This is a product that allows running applications in separate containers on the host-system. The main difference between a virtual machine and a container is, that the container system doesn’t come with the overhead of a full operating system. It only has the main component that is needed to run the application. Therefore, a container engine like docker is a very efficient way to run multiple applications in an isolated environment.

Docker Compose: Managing and configuring Docker containers can be difficult and complex. Docker Compose can manage multiple Docker containers. It’s managing this via a single configuration file that can run and configure multiple docker containers with a single command.

Mailcow: This is a collection of all necessary applications to create a fully-featured mail server. It uses all open-source projects like postfix, MySQL, dovecot, etc. and runs them in docker containers. It will manage the containers via docker-compose so that you don’t need to struggle with config files yourself.


3. Install docker on Ubuntu 18.04 LTS

First, we need to install docker on our Linux server. If you have trouble or want to use another distro/operating system, you can also refer to the official docker documentation: https://docs.docker.com/install/

1. (Optional) Remove old docker installation

It is recommended, to first uninstall old docker installations. If you already have docker set up and running correctly or never used it before, you can skip this step.

$ sudo apt remove docker docker-engine docker.io containerd runc
2. Install docker repository
$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
$ sudo apt update
3. Install docker
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
4. Check if docker was installed successfully

If everything was successful you can test docker, with this simple command

$ sudo docker run hello-world

4. Install docker-compose on Ubuntu 18.04 LTS

1. Check latest stable release

Docker-compose has no installation package for apt. You may check the latest stable release version first on the official website

https://docs.docker.com/compose/install/

2. Download docker-compose

Download the latest version (in this case it is 1.25.5, this may change whenever you read this tutorial!)

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
3. Make directory executable
$ sudo chmod +x /usr/local/bin/docker-compose
4. Check if docker-compose is working correctly

If everything was successful you can test docker-compose with this command.

$ sudo docker-compose --version

5. Install mailcow

1. Clone mailcow into the /opt folder

$ cd /opt
$ sudo git clone https://github.com/mailcow/mailcow-dockerized
$ cd mailcow-dockerized

2. Generate your configuration file and follow the steps below.

$ sudo ./generate_config.sh

3. Enter your mailserver FQDN (this is your mailserver hostname, not your domain name)

4. Select your timezone

5.1. (Optional) Insert custom SSL certificate

If you start “mailcow” it will automatically generate and request a letsencrypt certificate for your domains. If you don’t want that, but instead use your own certificate you need to modify the mailserver.conf and change the line to:

SKIP_LETS_ENCRYPT=y

Afterward, you can insert your own SSL certificate and replace the files cert.pem and key.pem in the “/opt/mailcow-dockerized/data/assets/ssl” directory

Refer to: https://mailcow.github.io/mailcow-dockerized-docs/firststeps-ssl/

5.2. Start mailcow

You can start your mail server with the following command. Note, that you need to be inside the “/opt/mailcow-dockerized” directory to execute the docker-compose.yml file

$ sudo docker-compose up -d

6. Configure mailcow

When all services are started successfully, you can now login to the admin dashboard and configure your domain, mailboxes, aliases, etc.

The admin dashboard can be accessed by https://<fqdn>

The default user and password are: admin moohoo

mail cow admin dashboard example

*-ADVERTISEMENT: You can also use a service like LastPass to store any administrative passwords in a secure and easy way. It also offers you two-factor authentication.

s

7. Set up your domain(s)

You need to set up your domain first at “Configuration -> Mail Setup -> Domains

example of domain setup

8. Set up your mailboxes

If you want to configure your mailboxes, you can add them at “Configuration -> Mail Setup -> Mailboxes

example of mailbox setup

9. What else needs to be done?

Now, I hope if everything worked fine, you got your mail server installed up and running! There might be other tasks to do in order to make it functional for a production environment. It now really depends on your special needs and infrastructure and can’t be generally suggested. Here are some ideas, what else you might need to do.

  • Add additional domains
  • Add mailbox aliases
  • Configure your public MX record
  • Add SPF record and set up DKIM and DMARC entry
  • Send and receive test-emails

Learn more about mail server DNS records

15 thoughts on “Install a mail server on Linux in 10 minutes – with docker, docker-compose, and mailcow”

  1. Hi, your tutorial is extremely efficient !

    I have a question : I want to add another domain (main) to let’s encrypt in this composer. How can I manage to do that ?

    Reply
  2. Thank you for opening our eyes to this perfect solution. I also liked the explanatory video on youtube. It is based on ubuntu and I would like to know if you have a procedure for a VPS server based on Centos 7.4. Thanks again .

    Reply
  3. Thanks for the tutorial! I am getting some problems after running “sudo docker-compose up -d”. It is giving me many errors: for example –

    ERROR: for unbound-mailcow Cannot start service unbound-mailcow: error while creating mount source path ‘/opt/mailcow-dockerized/data/conf/unbound/unbound.conf’: mkdir /opt/mailcow-dockerized: read-only file system

    ERROR: for watchdog-mailcow Cannot start service watchdog-mailcow: error while creating mount source path ‘/opt/mailcow-dockerized/data/assets/ssl’: mkdir /opt/mailcow-dockerized: read-only file system

    Plus a few more with the ” read-only file system” issue. Any Ideas as to how this can be corrected would be greatly appreciated?
    Many thanks

    Reply
    • I would check if the mount point is really read-only. This command should list all read-only mountpoints in your filesystem. If /opt is read-only you would need to remount it as rw. If not there probably is an issue with permissions on this folder.
      awk '$4~/(^|,)ro($|,)/' /proc/mounts

      Reply
  4. Hi,
    Love your tutorial, but I have a problem. I can send emails from this server to a gmail account, but when I try to do the same the other way round, my mail server does not recieve the email. What can I do?

    Reply

Leave a Comment

I accept the Privacy Policy