Contents

Setup an email server using Docker

Updated on 2020-06-14
Setup Email server using docker instance of

Poste.io

Note: Steps in this article have been followed on Ubuntu 18.04 server machine.

Setting up of email server is considered to be one of the most difficult tasks to do. There have been number of efforts to make this process easy and secure. Some of the notable projects are:

By far the simplest server to setup that I found was Poste.io. Here is how I did it.

  • 64bit linux distribution

  • CPU and free RAM

    Poste.io did not mention minimum requirement in this case. I myself used 2 CPU 8GB RAM machine.

  • NGINX

    You can of course use Apache. This tutorial only uses Nginx.

  • Docker

  • Docker-Compose

  1. Create user
  2. Install Nginx
  3. Setup Firewall
  4. Install Docker
  5. Install Docker-Compose
  6. Create docker-compose.yml
  7. Instantiate docker instance
  8. Configure Nginx as reverse proxy
1
adduser [username]

follow the steps in the prompt, then assign sudo privileges by executing the following command.

1
usermod -aG sudo [username]

Install Nginx using following commands:

1
2
3
4
sudo apt update
sudo apt install nginx
sudo systemctl restart nginx
sudo systemctl status nginx

List all applications' profiles

1
sudo ufw app list

Allow ssh

1
sudo ufw allow OpenSSH

Enable ufw

1
sudo ufw enable

Allow Nginx on port 80 (HTTP) and 443(HTTPS)

1
sudo ufw allow 'Nginx Full'

Check status of ufw

1
sudo ufw status

Install docker using following commands:

1
2
3
4
5
6
7
8
sudo apt update
sudo apt install apt-transport-https ca-certificates curl 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 bionic stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce
sudo systemctl status docker

Install docker-compose using following commands:

1
2
3
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

Create docker-compose.yml in some folder and navigate in terminal to that folder. Lets assume, we choose to create a folder in /home directory.

1
2
3
sudo mkdir -p /home/poste.io
cd /home/poste.io
sudo touch docker-compose.yml

Enter the following content to docker-compose.yml file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
version: '3'

services:

  mailserver:
    image: analogic/poste.io
    container_name: poste
    restart: always
    ports:
      - "82:80"
      - "445:443"
      - "25:25"
      - "110:110"
      - "143:143"
      - "587:587"
      - "993:993"
      - "995:995"
      - "4190:4190"
    environment:
      - TZ=Europe/Prague
      - h=mail.[some-domain].com # enter the email server hosting address
      - HTTP_PORT=82
      - HTTPS_PORT=445
      - DISABLE_CLAMAV=TRUE
      - DISABLE_RSPAMD=TRUE
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/nginx/html/.well-known:/opt/www/.well-known
      - /data/mailserver:/dataroot

Please notice:

  • image is analogic/poste.io. Poste.io offers two docker images, one poste.io/mailserver is a PRO version and other analogic/poste.io is a FREE version. We will be using the free version.
  • We are mapping docker container’s port 80 with port 82 of our host machine. Similarly, 443 of container with 445 of our host. Since, host’s port 80 and 443 will be used by Nginx for reverse proxy.
  • restart: always tells docker service to run this container whenever OS starts.

To instantiate (in detached mode), execute:

1
sudo docker-compose up -d

You can instead execute the following command to see the logs:

1
sudo docker-compose up

Create an Nginx config file by executing:

1
sudo nano /etc/nginx/sites-available/mail.server

Enter the following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
server {

        server_name mail.[some-domain].com;

        access_log /var/log/nginx/mail.access.log;
        error_log /var/log/nginx/mail.error.log;

        location / {
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-Host $host:$server_port;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_buffering off;
            proxy_pass https://172.25.0.2:445/;
            proxy_redirect default;
        }
}

Please notice:

  • server_name is the subdomain we entered in step 6.
  • proxy_pass is the ip address of the poste container. You can find by executing docker inspect [container id].

Note: You can optionally use certbot to setup HTTPS.

Комментарии