ユーザ用ツール

サイト用ツール


サイドバー

このページの翻訳:



最近の更新



Tag Cloud

06_virtualization:05_container:04_docker_mailserver

04 Docker MailServer

メールサーバも普通にたてるより、Dockerで作った方が良いかも

https://github.com/docker-mailserver/docker-mailserver

1.先にSSL用意

今回は下記でSSLを用意しておく想定
10 Docker Let's Encrypt

2. git clone

cd /app/
git clone https://github.com/docker-mailserver/docker-mailserver.git

3.設定変更

cd /app/docker-mailserver/

volumesにSSL証明書を読み込むように書きを追加してあります。

 /var/lib/docker/volumes/app_certs/_data:/etc/letsencrypt/live

compose.yml

services:
  mailserver:
    image: ghcr.io/docker-mailserver/docker-mailserver:latest
    container_name: mailserver
    # Provide the FQDN of your mail server here (Your DNS MX record should point to this value)
    hostname: mail.hogehoge.com
    env_file: mailserver.env
    # More information about the mail-server ports:
    # https://docker-mailserver.github.io/docker-mailserver/latest/config/security/understanding-the-ports/
    # To avoid conflicts with yaml base-60 float, DO NOT remove the quotation marks.
    ports:
      - "25:25"    # SMTP  (explicit TLS => STARTTLS)
      - "143:143"  # IMAP4 (explicit TLS => STARTTLS)
      - "465:465"  # ESMTP (implicit TLS)
      - "587:587"  # ESMTP (explicit TLS => STARTTLS)
      - "993:993"  # IMAP4 (implicit TLS)
    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
      - /var/lib/docker/volumes/app_certs/_data:/etc/letsencrypt/live
    restart: always
    stop_grace_period: 1m
    # Uncomment if using `ENABLE_FAIL2BAN=1`:
    cap_add:
      - NET_ADMIN
    healthcheck:
      test: "ss --listening --tcp | grep -P 'LISTEN.+:smtp' || exit 1"
      timeout: 3s
      retries: 0

※Timezone設定は下記で行っている。

/etc/localtime:/etc/localtime:ro
# diff mailserver.env mailserver.env.org 
45c45
< POSTMASTER_ADDRESS=hoge@hogehoge.com
---
> POSTMASTER_ADDRESS=
91c91
< SPOOF_PROTECTION=1
---
> SPOOF_PROTECTION=
179c179
< ENABLE_DNSBL=1
---
> ENABLE_DNSBL=0
185c185
< ENABLE_FAIL2BAN=1
---
> ENABLE_FAIL2BAN=0
212c212
< SSL_TYPE=letsencrypt
---
> SSL_TYPE=
348c348
< ENABLE_SPAMASSASSIN=1
---
> ENABLE_SPAMASSASSIN=0
460c460
< ENABLE_POSTGREY=1
---
> ENABLE_POSTGREY=0

DKIM設定

DKIMが有効になっている事確認

# grep ENABLE_OPENDKIM mailserver.env
ENABLE_OPENDKIM=1

DKIMキー作成

# ./setup.sh config dkim

このファイル ./docker-data/dms/config/opendkim/keys/[DOMAIN]/mail.txt でキーを確認できる。

# cat ./docker-data/dms/config/opendkim/keys/fl8.jp/mail.txt 
mail._domainkey	IN	TXT	( "v=DKIM1; h=sha256; k=rsa; "
	  "p=MIIXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Tgp1lVoJ03zG47q356P55lOhXopy36UhItepO22Fps44E69zHOPvwCNaz9ZqpWGI4E/KJ34GzHjb3I/ff16vOb+nNFknNNT0tdTj8a854meEz81S1hXv0pQCnbjyyy+6B1XEbozAsWsgCcq"
	  "nvXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXn4Kc6f1cSl9P4knXbo0mau2WDoceUyZTXNdZ1CQT+zoMN/B89B4cOong3Cq6AqWGMX/kmyHYaBzBPJUnO0be2E0InomGptyGE7Ni1yBV8Jw/BE9RhpXF+wN+CYnD1M7CNo2St9lQ"
	  "cXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXFGIofzGAaNVr4PIhtNoqj6qRAG+MHGduAYWBn8rSYfzhGQgWZTRLTo2bVxDQoLc5T82/tGq+CC1IBFsOZemlAp/gj/51zUDzsNx3YXsgs8s5Ef+eECAwEAAQ==" )  ; ----- DKIM key mail for fl8.jp

※上記だと、““で区切らてますが、これをDNSに登録する時は全て繋げて1行で登録します。

# dig mail._domainkey.fl8.jp TXT
mail._domainkey.fl8.jp.	120	IN	TXT	"v=DKIM1; h=sha256; k=rsa; p=MIIXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Tgp1lVoJ03zG47q356P55lOhXopy36UhItepO22Fps44E69zHOPvwCNaz9ZqpWGI4E/KJ34GzHjb3I/ff16vOb+nNFknNNT0tdTj8a854meEz81S1hXv0pQCnbjyyy+6B1XEbozAsWsgCcqnvXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXn4Kc6f1cSl9P4knXbo0mau2WDoceUyZTXNdZ1CQT+zoMN/B89B4cOong3Cq6AqWGMX/kmyHYaBzBPJUnO0be2E0InomGptyGE7Ni1yBV8Jw/BE9RhpXF+wN+CYnD1M7CNo2St9lQcXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXFGIofzGAaNVr4PIhtNoqj6qRAG+MHGduAYWBn8rSYfzhGQgWZTRLTo2bVxDQoLc5T82/tGq+CC1IBFsOZemlAp/gj/51zUDzsNx3YXsgs8s5Ef+eECAwEAAQ=="

4.起動

# docker-compose up -d

5. Procmail利用の場合

通常だとProcmail利用じゃないので、Procmailで配送するよう変更する場合imageを新たに作ります。

main.cf

# Mail directory
virtual_transport = lmtp:unix:/var/run/dovecot/lmtp
virtual_mailbox_domains = /etc/postfix/vhost
virtual_mailbox_maps = texthash:/etc/postfix/vmailbox
virtual_alias_maps = texthash:/etc/postfix/virtual

↓

# Mail directory
virtual_transport = procmail
virtual_mailbox_domains = /etc/postfix/vhost
virtual_mailbox_maps = texthash:/etc/postfix/vmailbox
virtual_alias_maps = texthash:/etc/postfix/virtual
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

こちらのやり方の方が良かった

これを変えると、「virtual_transport = lmtp:unix:/var/run/dovecot/lmtp」 → 「virtual_transport = procmail」に変わります。

# cat mailserver.env | grep POSTFIX_DAGENT
POSTFIX_DAGENT=

↓

# cat mailserver.env | grep POSTFIX_DAGENT
POSTFIX_DAGENT=procmail

master.cf

procmail  unix  -       n       n       -       -       pipe
  flags=R user=docker argv=/usr/bin/procmail -t -m USER=${user} DOMAIN=${nexthop} /tmp/docker-mailserver/procmailrc

procmailrc

docker-data/dms/config/procmailrc

SHELL=/bin/bash
PATH=/usr/bin:/bin
DROPPRIVS=yes

MAILDIR=/var/mail/$DOMAIN/$USER
DEFAULT=$MAILDIR/

# ロックファイル
LOCKFILE=/var/mail/.procmail.lock

# ログ出力先
LOGFILE=/var/mail/.procmail.log

# 詳細ログ出力
VERBOSE=OFF

## 以下にprocmailレシピを書いていく
:0 E
* ^From:.*hoge@junkmail.com
$MAILDIR/.Junk/

Dockerfile

procmailだけ追加インストール

RUN <<EOF
  rm -rf /usr/share/locale/*
  rm -rf /usr/share/man/*
  rm -rf /usr/share/doc/*
  update-locale
  apt update
  apt install -y procmail
EOF

build

DOCKER_BUILDKIT=1だけ入れて、build

DOCKER_BUILDKIT=1 docker image build -t mailserver2 .

procmail使った場合のエラー

こんなエラー出ます。

mailserver  | 2024-02-06T11:08:45.394582+09:00 mail postfix/pipe[119008]: 5AC87A5145: to=<XXXXXX@fl8.jp>, relay=procmail, delay=0.02, delays=0.01/0/0/0.01, dsn=2.0.0, status=sent (delivered via procmail service (procmail: Couldn't chdir to "/home/docker" procmail: Couldn't chdir to "/home/docker"))
mailserver  | 2024-02-06T11:18:40.840460+09:00 mail postfix/pipe[120188]: C5569A5147: to=<XXXXXX@fl8.jp>, relay=procmail, delay=0.03, delays=0.01/0.01/0/0.01, dsn=2.0.0, status=sent (delivered via procmail service (procmail: Couldn't chdir to "/home/docker" procmail: Couldn't chdir to "/home/docker"))
mailserver  | 2024-02-06T11:26:00.556709+09:00 mail postfix/pipe[121003]: 7DC92A5148: to=<XXXXXX@fl8.jp>, relay=procmail, delay=0.04, delays=0.01/0.02/0/0.02, dsn=2.0.0, status=sent (delivered via procmail service (procmail: Couldn't chdir to "/home/docker" procmail: Couldn't chdir to "/home/docker"))

対応

これはprocmailを実行するdockerユーザの、homeディレクトリが存在しないからエラー出ています。

下記で出なくなります。

docker-compose exec mailserver mkdir -p /home/docker

6.Spamassassin

cronで.Junkのメールを日々学習させておく

0 2 * * * docker exec mailserver sa-learn --spam /var/mail/example.com/username/.Junk --dbpath /var/mail-state/lib-amavis/.spamassassin
06_virtualization/05_container/04_docker_mailserver.txt · 最終更新: 2024/02/16 15:19 by matsui