To achieve delivering incoming mail to custom directories based on recipient domains with Postfix, you don't necessarily need to use multiple Postfix instances. You can achieve this using virtual alias maps and virtual transport maps.
I'm going to explain how to do this, but I would encourage you to use a database server approach, instead of files on disk. There are even docker images ready to be deployed that are more "future-compatible".
OK.
First: backup your current configuration.
Then, configure postfix for virtual domains.
I use vim for editing, you might want to use vi, nano...other.
Edit Postfix main configuration file:
sudo vim /etc/postfix/main.cf
Add or modify the following settings:
virtual_mailbox_domains = domain1.com, domain2.com
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_alias_maps = hash:/etc/postfix/virtual
virtual_transport = virtual
Then, define mailboxes and aliases by creating the vmailbox file to define the mailbox locations:
sudo vim /etc/postfix/vmailbox
Add the entries for the users and their corresponding mail directories:
user1@domain1.com /some/custom/MailDir/user1/
user2@domain2.com /different/custom/MailDir/user2/
user3@domain1.com /custom/user3/domain1/mail/
user3@domain2.com /different/user3/domain2/mail/
Save and close the file.
Now, create the virtual alias file (if needed) for any virtual aliasing:
sudo vim /etc/postfix/virtual
Example content:
user1@domain1.com user1@domain1.com
user2@domain2.com user2@domain2.com
Save and close the file.
Run postmap to hash these files:
sudo postmap /etc/postfix/vmailbox
sudo postmap /etc/postfix/virtual
Now, configure Dovecot for virtual mailboxes, edit dovecot configuration:
sudo vim /etc/dovecot/dovecot.conf
Make sure you've got the following:
mail_location = maildir:/var/mail/vhosts/%d/%n
Configure Dovecot to use Postfix's virtual user database:
sudo vim /etc/dovecot/conf.d/auth-passwdfile.conf.ext
Make sure you've got:
passdb {
driver = passwd-file
args = /etc/dovecot/users
}
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
}
Create the Dovecot users file:
sudo vim /etc/dovecot/users
Add the users:
user1@domain1.com:{plain}password1
user2@domain2.com:{plain}password2
user3@domain1.com:{plain}password3
user3@domain2.com:{plain}password4
Save and close the file.
Set permissions:
sudo chown -R vmail:vmail /var/mail/vhosts
Then restart Postfix and Dovecot
sudo systemctl restart postfix
sudo systemctl restart dovecot
Explanation:
Virtual Domains: The virtual_mailbox_domains setting defines the domains handled by this Postfix setup.
Mailbox Locations: The virtual_mailbox_maps file (/etc/postfix/vmailbox) defines where the mail for each user/domain combination should be delivered.
Dovecot Configuration: Dovecot is configured to look up users in a password file and map them to their mail directories based on %d (domain) and %n (username).
There are other tutorials in case mine isn't comprehensive enough.
Good luck!