Many of the recipes that follow require email access of some kind. It's normally possible to use a hosted service such as SendGrid, or just a gmail account. If (like me) you'd like to self-host email for your stacks, then the following recipe provides a full-stack mail server running on the docker HA swarm.
Of value to me in choosing docker-mailserver were:
- Automatically renews LetsEncrypt certificates
- Creation of email accounts across multiple domains (i.e., the same container gives me mailbox firstname.lastname@example.org, and email@example.com)
- The entire configuration is based on flat files, so there's no database or persistence to worry about
docker-mailserver doesn't include a webmail client, and one is not strictly needed. Rainloop can be added either as another service within the stack, or as a standalone service. Rainloop will be covered in a future recipe.
- Docker swarm cluster with persistent shared storage
- Traefik configured per design
- LetsEncrypt authorized email address for domain
- Access to manage DNS records for domains
Setup data locations¶
We'll need several directories to bind-mount into our container, so create them in /var/data/docker-mailserver:
1 2 3 4
Get LetsEncrypt certificate¶
Decide on the FQDN to assign to your mailserver. You can service multiple domains from a single mailserver - i.e., firstname.lastname@example.org and email@example.com can both be served by mail.example.com.
The docker-mailserver container can renew our LetsEncrypt certs for us, but it can't generate them. To do this, we need to run certbot (from a container) to request the initial certs and create the appropriate directory structure.
In the example below, since I'm already using Traefik to manage the LE certs for my web platforms, I opted to use the DNS challenge to prove my ownership of the domain. The certbot client will prompt you to add a DNS record for domain verification.
1 2 3 4
docker-mailserver comes with a handy bash script for managing the stack (which is just really a wrapper around the container.) It'll make our setup easier, so download it into the root of your configuration/data directory, and make it executable:
1 2 3
Create email accounts¶
For every email address required, run
./setup.sh email add <email> <password> to create the account. The command returns no output.
You can run
./setup.sh email list to confirm all of your addresses have been created.
Create DKIM DNS entries¶
./setup.sh config dkim to create the necessary DKIM entries. The command returns no output.
Examine the keys created by opendkim to identify the DNS TXT records required:
1 2 3 4
You'll end up with something like this:
1 2 3 4
Create the necessary DNS TXT entries for your domain(s). Note that although opendkim splits the record across two lines, the actual record should be concatenated on creation. I.e., the DNS TXT record above should read:
Setup Docker Swarm¶
Create a docker swarm config file in docker-compose syntax (v3.2 - because we need to expose mail ports in "host mode"), something like this:
I share (with my patreon patrons) a private "premix" git repository, which includes necessary docker-compose and env files for all published recipes. This means that patrons can launch any recipe with just a
git pull and a
docker stack deploy 👍
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
Setup unique static subnets for every stack you deploy. This avoids IP/gateway conflicts which can otherwise occur when you're creating/removing stacks a lot.
A sample docker-mailserver.env file looks like this:
1 2 3 4 5 6 7 8 9
Launch the mail server stack by running
docker stack deploy docker-mailserver -c <path-to-docker-mailserver.yml>
Chef's Notes 📓¶
One of the elements of this design which I didn't appreciate at first is that since the config is entirely file-based, setup.sh can be run on any container host, provided it has the shared data mounted. This means that even though docker-mailserver was not designed with docker swarm in mind, it works perfectl with swarm. I.e., from any node, regardless of where the container is actually running, you're able to add/delete email addresses, view logs, etc.
Tip your waiter (support me) 👏¶
Did you receive excellent service? Want to make your waiter happy? (..and support development of current and future recipes!) See the support page for (free or paid) ways to say thank you! 👏
Flirt with waiter (subscribe) 💌¶
Want to know now when this recipe gets updated, or when future recipes are added? Subscribe to the RSS feed, or leave your email address below, and we'll keep you updated. (double-opt-in, no monkey business, no spam either - check the archive for proof!)