6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker/alpineでどこにも配送しないsmtpサーバーを作る

Posted at

ゴール

alpineのコンテナに postfixをインストールしてどこにも配送しないsmtpサーバーを作る。
さらにsaslauthdをインストールし、コンテナの外からSMTP AUTHが使えるようにする。

対象環境

windows 10 で動作する docker for windows

コンテナ起動

alpine:latestイメージを使って新しいコンテナを起動する。
コンテナ外からSMTPにアクセスするためにポート25を開ける。

PS > docker run -it -d -p 25:25 --name my_alpine alpine

パッケージインストール

必要なのはpostfix,saslauthd関連。

# apk update
# apk add postfix postfix-pcre
# apk add mailx
# apk add cyrus-sasl cyrus-sasl-crammd5
# apk add rsyslog

rsyslogを入れてるのは postfixが出力するログを確認するため。まじめなコンテナを作る場合はsyslog専用コンテナを作る方がよいかも。

postfixの設定

前に設定した テスト用に外に一切配送しないSMTPサーバーを作る の設定を流用して外に配送しないようにする。

vi /etc/postfix/main.cf

/etc/postfix/main.cf
myhostname = localhost.localdomain
mydomain = localdomain
myorigin = $myhostname
mydestination = $myhostname, $mydomain

smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks,
     permit_sasl_authenticated,
     reject_unauth_destination
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_client_restrictions=permit_sasl_authenticated,reject

alias_maps = hash:/etc/postfix/aliases,pcre:/etc/postfix/aliases.regexp
transport_maps = pcre:/etc/postfix/transport_maps
smtpd_sasl_application_name = smtpd
broken_sasl_auth_clients = yes

vi /etc/postfix/aliases.regexp

/etc/postfix/aliases.regexp
/(?!^root$|^mailuser$)^.*$/ mailuser

vi /etc/postfix/transport_maps

/etc/postfix/transport_maps
/^.*@.*$/ local

エイリアス設定を有効にするために newaliases を実行する必要がある。
普通のOSだとpostfixをインストールしたら関連ディレクトリとか勝手に作ってくれるけど、そんなにうまくはいかないのでメールボックス用のディレクトリも作る

# newaliases
# mkdir /var/mail

saslauthdの設定

外からのメール送信をSMTP AUTHで認証したいので、そのように設定する。

vi /etc/sasl2/smtp.conf

/etc/sasl2/smtp.conf
pwcheck_method: auxprop
auxprop_plugin: sasldb
mech_list: PLAIN LOGIN CRAM-MD5 DIGEST-MD5
log_level: 7

送信用ユーザー作成

メール送信に使うユーザーを作る。
ローカルユーザーだけ作っても外からは認証できないのでsasl用のユーザーも作る

# adduser mailuser
#  saslpasswd2 -c -u localhost.localdomain mailuser

saslユーザーDBをpostfixが読み込めるように所有者を変更する

# chown postfix /etc/sasldb2

サービスを開始する

# postfix start
# saslauthd -a shadow
# rsyslogd -n &

それぞれ supervisorとかなんとかで起動するようにした方が良いのだけど、今のところここまで。

この後

これらのタスクを Dockerfileにまとめる。

その他

postfixとか saslauthd とかdocker hubにいろいろイメージはあるわけだけど、どうしてここまで苦労して作るのか、と言えば思いがけず外にテスト用メールを漏らしたくないから。

あと、現在のalpine:latest でインストールできる cyrus-sasl には pam認証がない。必要ならソースからビルド、ということになる。

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?