complete mail server with docker


the host hosting the mail server is

the domain is the part after the @. it means that emails will be sendable to *


create this docker-compose.yml file :

    image: tvial/docker-mailserver:stable
    restart: always
    container_name: mail
        # smtp (accept starttls) for transfer
        - "25:25"
        # esmtp (accept starttls) for submission
        - "587:587"
        # esmtp (forced encryption) for submission
        # - "465:465"
        # imap4 (accept starttls) for retrieval
        - "143:143"
        # imap4 (forced encryption) for retrieval
        - "993:993"
        # pop3 (accept starttls) for retrieval
        # - "110:110"
        # pop3 (forced encryption) for retrieval
        # - "995:995"
        - NET_ADMIN
        - SYS_PTRACE
        - ./mail/maildata:/var/mail
        - ./mail/mailstate:/var/mail-state
        - ./mail/maillogs:/var/log/mail
        - ./mail/config/:/tmp/docker-mailserver/
        - ./traefik/acme.json:/etc/letsencrypt/acme.json:ro
        - "SSL_TYPE=letsencrypt"
        - "SPOOF_PROTECTION=1"

# only useful to generate the https certificate used in mailserver
    image: containous/whoami
    container_name: mailssl
        - traefik.enable=true
        - traefik.http.routers.mail.rule=Host(``)
        - traefik.http.routers.mail.tls.certresolver=le
        - traefik.http.routers.mail.entrypoints=websecure
        - traefik.http.routers.mail.middlewares=securityheaders

    image: hardware/rainloop
    restart: always
    container_name: webmail
        - mail
        - ./webmail:/rainloop/data
        - LOG_TO_STDOUT=true
        - traefik.enable=true
        - traefik.http.routers.webmail.rule=Host(``)
        - traefik.http.routers.webmail.tls.certresolver=le
        - traefik.http.routers.webmail.entrypoints=websecure
        - traefik.http.routers.webmail.middlewares=securityheaders

get the setup script

curl -o; chmod a+x ./

check the dns of your domain :

# the A record tells what is the ip of the server
IN A      X.X.X.X
mail IN A      X.X.X.X

# the mx record tells which server to call when you need to send emails ending like this
# they will call port 25 on this server
IN MX     10

create an email account

execute this command

bash email add xxx

it will create a line in the file

emacs config/|xxxxxxxxxx

catchall address:

now every mails sent to * will be redirected to

emacs config/

test working

to test that local smtp is working and that the mailserver delivers the mails to the mailbox :

docker-compose up -d --force-recreate mail

swaks -t -f --header "Subject: foo"

will show this for success

<-  250 2.0.0 Ok: queued as EEF106F804EE

you can see the delivered mail in maildata/

you can download this mail from your phone or from a webmail.

do not send directly our mails to spam :)

DKIM to sign the message

run this to create keys

bash config dkim

it will create this file containing what you should add in your dns

emacs ./config/opendkim/keys/

then add this in dns

mail._domainkey        IN TXT    ( "v=DKIM1; h=sha256; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8XOWBJea+/0q8FOnbdWx7micDDhXv5r4dYlF6fLnA8nOzm6OAUZpppD8nXZN5SbBU9UxVRJcmBe/cVUaXbgL9vo5TxcjYpEOpx5/1nd3GsgNsPm9Ur8DhOTBN0yKSRfbRNWJXSL273jP7j/9heQI3ah4KUOh3LIjT5xCGyd3vlZTZ2/Qio" "V0xC5zckkS7HNaH3Bi655fMvlXhULM79mvw2aJAG83JfPc4ybwAPthFTIMsxyQ9sAIiEpBP2+OHscL4H42ul0MoAxrNTUW468QD5RuruKA3z5IBBngwKu7EELJQduSoQV1XfZB7rQCOMjtFCs6jpHu9Izr2CyUgBBeewIDAQAB" )

the spf record tells which server are authorized to send emails

authorise all the servers listed in mx. "~" means no problem if its another server
consider switching to "-" when everything works

IN TXT    "v=spf1 mx ~all"

dmarc record uses dkim and spf for more quality

_dmarc                 IN TXT    "v=DMARC1; p=none;;; sp=none; ri=86400"

now you can restart and test

docker-compose up -d --force-recreate mail


its a webmail, it connects to the server to retrieve emails.
(you can also configure email reception directly on your phone)

use default password to first log

then change password

then add a domain :

    server :
    port: 143
    security : starttls

    server :
    port: 587
    security : starttls
    use authentification

the whitelist can stay empty

multiple domains

if you want to add a new domain to the existing server

configure mx on to point to

add a new account for the new domain :

bash email add xxx

generate dkim agaim

add dkim + spf + dmarc in your dns

generate the ssl certificate :
replace this line



traefik.http.routers.mail.rule=Host(``, ``, )