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: 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

*-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.
7. Set up your domain(s)
You need to set up your domain first at “Configuration -> Mail Setup -> Domains“

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

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.
Thank you!!
Also, thank’s to you 😉 Mailcow is so nice, keep it going!
where are the emails saved?
I get a 502 page, preparing, and doesn’t go further than that. Any ideas?
Check out the docker container logs, just use
docker-compose logs
and check out the common problems and troubleshooting documentation on https://mailcow.github.io/mailcow-dockerized-docs/debug-common_problems/You may also have a look at the mailcow forum, the community is very helpful and nice!
Excellent,
It’s so easy and very useful.
Tks
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 ?
Thank you! 🙂 Usually, mailcow tries to obtain a letsencrypt cert for all domains that are added to the server.
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 .
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
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
Thanks for getting back to me, I will look into it!
Thanks for getting back to me, I will look into it and see what I come up with?
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?
Hi it would be very helpful if you can make video how to make work mailcow nginx (for mail) and apache2 (for website) in the same server.
Hello I don’t know about lot Docker and want to install mail cow but thinking about future if I will loss docker containers then I can loss everything. Please May you tell me about it
Hi, I’ve recently done a video about docker, where I explain some of the fundamentals of containers. Maybe this is helpful to you: https://www.youtube.com/watch?v=_trJf3GbZXg
Hi,
Thank you very much for the tutorial! But I have a problem. I have set all the dns records. I can now receive emails but I cannot send. Could you advice me where to look for the possible issue based on priority?
Hi, I’ve made another tutorial about mail server DNS records, there I talk about this https://youtu.be/o66UFsodUYo. Check your PTR and MX record and if you’re on a blacklist.