(This article was crafted with assistance from Bing AI.)
In this tutorial, we’ll walk through the process of setting up a mail server using Docker Compose and Dovecot. We’ll cover the configuration of both the Mail Transfer Agent (MTA) and the Mail Delivery Agent (MDA).
[LINK TO CLONE THE REPOSITORY OR DOWNLOAD ZIP FROM GITHUB]
Prerequisites
Before we begin, make sure you have the following prerequisites:
- Docker installed on your system.
- Basic knowledge of Docker Compose.
Concepts of mailing and containerization
Certainly! Let’s dive into each of these terms:
- MDA (Mail Delivery Agent): An MDA is responsible for delivering email messages to the recipient’s mailbox. It receives messages from the Mail Transfer Agent (MTA) and places them in the appropriate mailbox (e.g., using protocols like IMAP or POP3).
- MTA (Mail Transfer Agent): The MTA handles the routing and delivery of email messages between mail servers. It uses protocols like SMTP (Simple Mail Transfer Protocol) to transfer emails from the sender’s server to the recipient’s server.
- SPF (Sender Policy Framework): SPF is an email authentication method that allows domain owners to specify which servers are authorized to send emails on their behalf. SPF records list valid IP addresses for sending emails from a domain1.
- DKIM (DomainKeys Identified Mail): DKIM adds a digital signature to outgoing emails, allowing recipients to verify that the email came from the claimed domain. It uses public-key cryptography to sign and verify messages.
- DMARC (Domain-based Message Authentication Reporting and Conformance): DMARC builds upon SPF and DKIM. It instructs email servers on how to handle emails that fail SPF or DKIM checks. Domains use DMARC policies to specify actions like marking as spam or rejecting failed emails1.
- Postfix: Postfix is a popular open-source MTA used for routing and delivering email messages. It’s highly configurable and widely used in email server setups.
- Dovecot: Dovecot is an MDA and IMAP/POP3 server. It retrieves emails from the mailbox and serves them to clients (such as email clients or webmail interfaces).
- Docker: Docker is a containerization platform that allows you to package applications and their dependencies into lightweight containers. It simplifies deployment and ensures consistent environments.
- Bridge Network: In Docker, a bridge network connects containers on the same host, allowing them to communicate securely. It provides isolation and manages IP addresses for containers.
- SASL Authentication (Simple Authentication and Security Layer): SASL is a framework for adding authentication support to protocols. It’s commonly used for authenticating email clients with SMTP servers.
- Public and Private Key: Public-key cryptography uses a pair of keys: the public key (shared openly) and the private key (kept secret). These keys are used for encryption, decryption, and digital signatures.
- Signature and Certificate: A digital signature is created using a private key to verify the authenticity and integrity of data. Certificates (such as SSL/TLS certificates) contain public keys and additional information about the owner, issued by a Certificate Authority (CA).
- Docker-Compose.yml: A YAML file used to define multi-container Docker applications. It specifies services, networks, volumes, and other configurations for a Docker Compose project.
- Dockerfile: A text file that defines how to build a Docker image. It includes instructions for installing software, setting up configurations, and creating the image layers.
Most important commands we used during this setup
telnet localhost 25
: This command initiates a Telnet session to the local machine on port 25, which is typically used for SMTP (Simple Mail Transfer Protocol) communication. It allows you to interact with an email server.nano docker-compose.yml
: Thenano
command opens thedocker-compose.yml
file in a text editor called Nano. This file is commonly used to define services and their configurations for Docker containers.nano dovecot.conf
: Similar to the previous command, this opens thedovecot.conf
file in Nano. Dovecot is an email server software, and its configuration file specifies settings related to IMAP and POP3 protocols.nano Dockerfile
: Opens theDockerfile
in Nano. Dockerfiles are used to create custom Docker images by specifying instructions for building a container.docker-compose up --build
: This command starts the services defined in thedocker-compose.yml
file and rebuilds the images if necessary.docker container exec -i
: Executes a command inside a running Docker container. The-i
flag allows interactive input.chmod 777 data/ -R
: Changes permissions recursively for thedata/
directory, giving read, write, and execute permissions to everyone.cat data/spool/mail/*
: Displays the contents of all files in thedata/spool/mail/
directory. These files likely contain email messages.nano main.cf
: Opens themain.cf
configuration file for Postfix, a popular mail transfer agent (MTA).ufw status
: Checks the status of the Uncomplicated Firewall (UFW) rules.
Docker Compose
Let’s start by creating a docker-compose.yml
file that defines our services:
In this configuration, we define two services: mta
(Mail Transfer Agent) and mda
(Mail Delivery Agent). The mta
service handles SMTP traffic, while the mda
service handles POP3 and IMAP traffic.
Dockerfile.mta
This Dockerfile sets up a Postfix mail server with specific configurations for sending emails through Gmail’s SMTP server. This includes additional features related to SMTP, DKIM (DomainKeys Identified Mail), and logging. Let’s break down the steps:
- Base Image: It starts with the official Ubuntu base image.
- Update and Install Packages:
- It updates the package list and installs several packages related to Postfix, including
postfix
,rsyslog
,mailutils
, and various SASL-related libraries. - The
apt clean
command removes unnecessary files after installation.
- It updates the package list and installs several packages related to Postfix, including
- Configure
master.cf
:- Adds a configuration line to
master.cf
to enable thesubmission
service for SMTP.
- Adds a configuration line to
- Client Authentication:
- Configures Postfix to use Dovecot for authentication (
smtpd_sasl_type = dovecot
). - Specifies the path for Dovecot (
smtpd_sasl_path
). - Disables TLS (
smtpd_use_tls = no
) and allows only authenticated users (smtpd_tls_auth_only = yes
).
- Configures Postfix to use Dovecot for authentication (
- External Relay Configuration:
- Copies a
sasl_passwd
file and creates a hash map from it. - Sets up TLS policy for Gmail (
tls_policy
). - Manages permissions for the TLS policy file.
- Copies a
- Certificates:
- Creates a directory for SSL certificates.
- Copies the SSL certificate and private key.
- Sets appropriate permissions.
- SASL Mechanisms:
- Configures SASL mechanisms for SMTP authentication.
- Relay Configuration:
- Sets Gmail as the relay host (
relayhost = [smtp.gmail.com]:587
). - Specifies authentication details (
smtp_sasl_password_maps
). - Sets TLS-related options.
- Sets Gmail as the relay host (
- SMTP Configuration:
- OpenDKIM Configuration:
- Creates directories for OpenDKIM keys.
- Configures various options in
opendkim.conf
, including syslog, permissions, auto-restart, and socket settings. - Copies DKIM keys into the container.
- Configures KeyTable, SigningTable, and TrustedHosts for OpenDKIM.
- Postfix Integration with OpenDKIM:
- Specifies milter protocol and action (
milter_protocol
andmilter_default_action
). - Sets up milters for SMTP (
smtpd_milters
andnon_smtpd_milters
).
- Specifies milter protocol and action (
- Logging Configuration:
- Configures rsyslog to capture Postfix logs (
/var/log/mail.log
).
- Configures rsyslog to capture Postfix logs (
- Email Handling Script:
- Copies the
save_email.sh
script into the container and makes it executable.
- Copies the
- Virtual Email Aliases:
- Defines virtual aliases for specific domains (
@gordarg.com
). - Maps these aliases to a local transport (
virtual_transport = local
).
- Defines virtual aliases for specific domains (
- Exposed Ports:
- Exposes SMTP ports 25 and 587.
Dockerfile.mda
This Dockerfile sets up a Dovecot mail server with specific configurations for POP3 and IMAP services.
- Base Image and Package Installation:
- It starts with the official Ubuntu base image.
- Installs
dovecot-core
,dovecot-pop3d
, anddovecot-imapd
.
- Working Directory and Configuration:
- Sets the working directory to
/etc/dovecot
. - Copies the Dovecot configuration file (
dovecot.conf
).
- Sets the working directory to
- SSL Configuration:
- Copies SSL certificates (
dovecot.pem
,dovecot.pem.private
, anddh.pem
). - Sets appropriate permissions.
- Updates the SSL configuration in
10-ssl.conf
.
- Copies SSL certificates (
- User Database and Permissions:
- Copies the user database file (
dovecot-users
). - Sets permissions for the user database.
- Copies the user database file (
- Log Files:
- Initializes log files (
dovecot-info.log
,dovecot.log
, anddovecot-debug.log
).
- Initializes log files (
- Exposed Ports:
- Exposes POP3 (110), IMAP (143), IMAPS (993), and an additional port (12345).
- User Setup:
- Creates a user (
vmail
) with specified UID and GID. - Sets the user’s shell to
/bin/bash
.
- Creates a user (
- Start Dovecot Service:
- Starts the Dovecot service and tails the log files.