This Ansible playbook automates the Docker-based deployment of iRedMail mail server using community Docker images.
Note: Docker deployment is community-maintained, not officially supported by iRedMail. For traditional installation, see iRedMail Ansible.
Create iredmail-docker-deploy.yml:
---
- name: Deploy iRedMail with Docker
hosts: iredmail
become: true
vars:
# Domain configuration
iredmail_domain: example.com
iredmail_hostname: mail.example.com
# Admin credentials
iredmail_admin_password: "secure_admin_password"
# Database configuration
mysql_root_password: "secure_mysql_password"
iredmail_mysql_password: "secure_iredmail_mysql_password"
# Docker configuration
docker_install_compose_plugin: true
iredmail_docker_path: /opt/iredmail-docker
tasks:
# Install Docker
- name: Install Docker prerequisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
state: present
update_cache: true
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
- name: Install Docker Engine
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
update_cache: true
- name: Enable and start Docker
systemd:
name: docker
state: started
enabled: true
- name: Add user to docker group
user:
name: "{{ ansible_user_id }}"
groups: docker
append: true
# Clone iRedMail Docker repository
- name: Clone iRedMail Docker repository
git:
repo: https://github.com/iredmail/docker-iredmail
dest: "{{ iredmail_docker_path }}"
version: master
# Create docker-compose.override.yml
- name: Create docker-compose.override.yml
copy:
dest: "{{ iredmail_docker_path }}/docker-compose.override.yml"
mode: '0644'
content: |
version: '3.8'
services:
iredmail:
environment:
- MYSQL_ROOT_PASSWORD={{ mysql_root_password }}
- IREDMAIL_MYSQL_PASSWORD={{ iredmail_mysql_password }}
- IREDMAIL_ADMIN_PASSWORD={{ iredmail_admin_password }}
- IREDMAIL_DOMAIN={{ iredmail_domain }}
- IREDMAIL_HOSTNAME={{ iredmail_hostname }}
ports:
- "25:25"
- "587:587"
- "465:465"
- "143:143"
- "993:993"
- "110:110"
- "995:995"
- "80:80"
- "443:443"
volumes:
- iredmail-vmail:/var/vmail
- iredmail-mysql:/var/lib/mysql
- iredmail-config:/etc/iredmail
# Start iRedMail containers
- name: Pull Docker images
command: docker compose pull
args:
chdir: "{{ iredmail_docker_path }}"
- name: Start iRedMail services
command: docker compose up -d
args:
chdir: "{{ iredmail_docker_path }}"
# Wait for services
- name: Wait for iRedMail to be ready
wait_for:
timeout: 180
delegate_to: localhost
# Configure firewall
- name: Configure UFW firewall
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
loop:
- "25"
- "587"
- "465"
- "143"
- "993"
- "110"
- "995"
- "80"
- "443"
# Verify installation
- name: Check iRedMail containers
command: docker compose ps
args:
chdir: "{{ iredmail_docker_path }}"
register: iredmail_status
- name: Display iRedMail status
debug:
var: iredmail_status.stdout
Create inventory.ini:
[iredmail]
mail.example.com ansible_user=root
[iredmail:vars]
ansible_python_interpreter=/usr/bin/python3
# Run the playbook
ansible-playbook -i inventory.ini iredmail-docker-deploy.yml
# With custom variables
ansible-playbook -i inventory.ini iredmail-docker-deploy.yml \
-e iredmail_domain=example.com \
-e iredmail_hostname=mail.example.com \
-e iredmail_admin_password="super_secure_password"
| Variable | Default | Description |
|---|---|---|
iredmail_domain |
Required | Email domain |
iredmail_hostname |
Required | Mail server FQDN |
iredmail_admin_password |
Required | iRedAdmin password |
mysql_root_password |
Required | MySQL root password |
iredmail_mysql_password |
Required | MySQL user password |
iredmail_docker_path |
/opt/iredmail-docker |
Installation directory |
https://mail.example.com/iredadmin/postmaster@example.com# A Record
mail.example.com. IN A <server-ip>
# MX Record
example.com. IN MX 10 mail.example.com.
# SPF Record
example.com. IN TXT "v=spf1 mx a -all"
# DKIM Record (get from iRedAdmin)
dkim._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=..."
# DMARC Record
_dmarc.example.com. IN TXT "v=DMARC1; p=quarantine"
cd /opt/iredmail-docker
# View status
docker compose ps
# View logs
docker compose logs -f
# Restart services
docker compose restart
# Database backup
docker compose exec iredmail mysqldump -u root -p iredmail > backup.sql
# Mail backup
docker run --rm \
-v iredmail-vmail:/vmail:ro \
-v $(pwd):/backup \
alpine tar czf /backup/vmail-backup.tar.gz -C /vmail .
⚠️ Community Docker Images: These Docker images are community-maintained and not officially supported by iRedMail.org.
⚠️ For Production: Consider using the traditional installation for production deployments.
Any questions?
Feel free to contact us. Find all contact information on our contact page.