In a Post last week we mentioned that a server failure caused the temporary loss of the company Website and Mail Server. The Post then went on to describe the conversion of the company website to a one built within a docker image rather than on a physical server. The aim of this post is to cover the virtualization of the other service which failed - the Email Server.
The general steps to creating the email server are similar to that of the web server:
- Find appropriate Docker image as a base.
- Create Folder structure.
- Create Docker/Docker-Compose File.
- Create SystemD service file.
- Import Backup Up User Mail Folders.
From a survey of various websites it seems that ’tvial/docker-mailserver:latest’ image is a good basis for a start. So on the server we created the following folder structure:
/home/services/mailserver
/config/
/maildata/
In the ‘/home/services/mailserver’ folder we created the following ‘docker-compose.yml’ file:
mail:
image: tvial/docker-mailserver:latest
# build: .
hostname: mail
domainname: riomhaire.com
container_name: mail
ports:
- "25:25"
- "143:143"
- "587:587"
- "993:993"
volumes:
- /home/services/mailserver/maildata:/var/mail
- /home/services/mailserver/config/:/tmp/docker-mailserver/
What we now need is to create the mail server docker image. This is fairly simple using docker-compose:
docker-compose create
With the mail container created we need to be create ‘postfix-accounts.cf’ in the ‘config’ folder which defines which email users. You could use a LDAP server, but since we are a small company with few user accounts using a file is OK. The way of adding a user (and their password) is done via the docker command:
docker run --rm \
-e MAIL_USER=user@domain \
-e MAIL_PASS=password \
-ti tvial/docker-mailserver:latest \
/bin/sh -c 'echo "$MAIL_USER|$(doveadm pw -s SHA512-CRYPT -u $MAIL_USER -p $MAIL_PASS)"' >> config/postfix-accounts.cf
Changing to a ldap based system or changing other dovecot configuration (such as upping maximum mail message size) requires creating a custom dovecot configuration file - so look at the dovecot configuration and the base docker image documentation.
So we have users - time to start the mail server:
docker-compose start
You need to then set up your router to point the ports 25, 143, 587 and 993 and then connect your mail client of choice at your mail server host. All very straight forward.
There are a number of ways of importing your old mail messages into the new container. The approach we used (since we backed up each users mail directory) was to mount them as ’local’ mail server within Thunderbird and then copy them across to the new container.
Finally we need to set up the container as a systemd service and then were almost done. Like in the Webserver Post we need to create a service file. In this case a ‘mail.service’ definition:
[Unit]
Description=Mail Service Container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a mail
ExecStop=/usr/bin/docker stop -t 2 mail
[Install]
WantedBy=default.target
and copy it to the ‘/etc/systemd/system’ folder and set the appropriate permissions. Finally all that remains is to enable the service via:
sudo systemctl enable mail
We hope this post has been useful.