In today's digital world, managing a reliable and secure mail server is crucial for any organization. With the rise of containerization technologies like Docker, setting up and managing a mail server has become more accessible and streamlined. In this article, we will walk through the process of setting up a mail server using Docker Mailserver, covering prerequisites, installation, configuration, and best practices to ensure a secure and efficient email system.
Docker Mailserver is a popular open-source mail server suite that simplifies the deployment and management of a mail server using Docker containers. It provides an all-in-one solution for handling SMTP, IMAP, and POP3 services, along with support for security features such as DKIM, SPF, and DMARC. This article will guide you through the complete setup of Docker Mailserver, from initial installation to fine-tuning your server for optimal performance and security.
Before diving into the setup process, ensure you have the following prerequisites:
mail.example.com
).Create a docker-compose.yml
file with the following content to define the mail server service:
version: "3"
networks:
mailserver:
name: mailserver
services:
mailserver:
image: docker.io/mailserver/docker-mailserver:13.0.1
container_name: mail.example.com
hostname: mail.example.com
env_file: mailserver.env
ports:
- "25:25" # SMTP
- "143:143" # IMAP4
- "465:465" # ESMTP
- "587:587" # ESMTP
- "993:993" # IMAP4
volumes:
- ./docker-data/dms/mail-data/:/var/mail/
- ./docker-data/dms/mail-state/:/var/mail-state/
- ./docker-data/dms/mail-logs/:/var/log/mail/
- ./docker-data/dms/config/:/tmp/docker-mailserver/
- /etc/localtime:/etc/localtime:ro
- /home/docker-user/traefik/acme.json:/etc/letsencrypt/acme.json:ro
restart: always
stop_grace_period: 1m
cap_add:
- NET_ADMIN
healthcheck:
test: "ss --listening --tcp | grep -P 'LISTEN.+:smtp' || exit 1"
timeout: 3s
retries: 5
networks:
- mailserver
Adjust any of the environment variables to better fit your configuration and needs.
Create the necessary directories for storing mail data, state, logs, and configuration:
mkdir -p docker-data/dms/mail-data
mkdir -p docker-data/dms/mail-state
mkdir -p docker-data/dms/mail-logs
mkdir -p docker-data/dms/config
Start the mail server container:
docker-compose up -d
Add an email address using the provided setup.sh
script:
./setup.sh email add admin@mail.example.com
Generate DKIM keys and configure your DNS:
./setup.sh config dkim
Add the DKIM TXT record to your DNS settings based on the output file located at ./docker-data/dms/config/opendkim/keys/mail.example.com/mail.txt
.
After configuring DKIM and SPF records, restart the container to apply changes:
docker-compose restart
mail.example.com
mail._domainkey.mail
v=DKIM1; h=sha256; k=rsa; p=MIIBIRANDOMudT8ywZatm/vF1Bofy3A+5iPhiIPJFmztU5mQuWcnu4Ml5WYQRP3UFyRANDOMUxNwqZfHsZ+Xl+uac+6AeUduDtRQSxtpDauNM4X/f7sGPlu3ExtoRANDOMrmVAM5NIPrjD1dhUcEYKaaCtbZ+OxZ688lsECgHa7dk1Zchv+8RpKO2nRvB2pUg/5Cls9eATVkkHh4qY0/cWPt8PmaBEtANgAPXeJxDz3Q/X4vS5W+2hsRANDOMcBxGBfo7VPLAnL87GDzG3HUONSYn0Ct6YJRANDOMAlYswiSuZQwIDAQAB
Use the generated DKIM keyv=spf1 mx a:mail.example.com include:mail.example.com +all
_dmarc
v=DMARC1; p=none; rua=mailto:admin@mail.example.com; ruf=mailto:admin@mail.example.com; sp=none; ri=86400
If you are experiencing issues with receiving emails, follow these detailed guidelines to troubleshoot and resolve the problem:
Check Configuration Files:
myhostname
and mydomain
variables are correctly set.utils/postfix.conf.example
Example File Linkfor configuration details. Check the IMPORTANT
section. Verify that these values are correctly set in the /etc/postfix/main.cf
file.
mydestination = localhost.$mydomain, localhost
myhostname = mail.example.com
mydomain = mail.example.com
Apply Changes:
postfix reload
telnet
or nc
to test connectivity to the mail server.Use online tools to test the configuration of your mail server. Some useful tools include:
Monitor the logs to troubleshoot issues:
docker-compose logs -f
Check logs located in docker-data/dms/mail-logs/
for detailed information.
Ensure that DKIM, SPF, and MX records are correctly configured using tools like: