: O. Yuanying

Docker 上にメールサーバを構築する (1)

構成

Docker で動作するメールサーバを構築する。 構成は以下。

yuanying/postfixadmin, yuanying/postfix, yuanying/dovecot という三つのイメージを用意して、 実際にはコンテナを三つ起動する。 アカウントやドメインの管理は postfixadmin を利用して MySQL 上にデータを置く。

以下は固定。

  • MySQL 実行コンテナ名: mysql
  • データベースプロバイダ: mysql
  • データベースポート: 3306
  • データベース名: postfix
  • データベースユーザ: postfix
  • 管理者メールアドレス: admin@${domain}

MySQL は別コンテナで起動しているが、もうすで用意されている物とする。

yuanying/postfixadmin

さくっと Dockerfile を書く。

Dockerfile

# docker build -t yuanying/postfixadmin .
# docker run -d --link mysql:mysql \
# -e "POSTFIX_MYSQL_PASSWORD=postfixpassword" \
# -e "POSTFIXADMIN_SETUP_PASSWORD=POSTFIXADMIN_SETUP_PASSWORD" \
# -h 'mail.fraction.jp' \
# -v /home/vmail:/var/vmail \
# -p 8080:8080 \
# yuanying/email-server
#
FROM ubuntu:precise
MAINTAINER O. Yuanying "yuan-docker@fraction.jp"

ENV DEBIAN_FRONTEND noninteractive
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install python-software-properties
RUN add-apt-repository -y ppa:ondrej/php5
RUN apt-get update
RUN apt-get install -y curl php5 php5-mysql php5-curl php5-imap

WORKDIR /opt
RUN curl http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.3.7/postfixadmin-2.3.7.tar.gz -L -O
RUN tar zxvf postfixadmin-2.3.7.tar.gz
RUN ln -s /opt/postfixadmin-2.3.7 /opt/postfixadmin

ADD config.inc.php /tmp/config.inc.php
RUN mv /tmp/config.inc.php /opt/postfixadmin/config.inc.php
ADD run /usr/local/bin/run
RUN chmod +x /usr/local/bin/run

EXPOSE 8080

CMD ["/usr/local/bin/run"]
  • php5.4 以降を使いたかったので ppa:ondrej/php5 を追加。
  • 設定ファイル config.inc.php はいじった物を ADD する。
  • postfixadmin 自体は /usr/local/bin/run で起動する。

config.inc.php

設定ファイルを修正した点は以下。 設定が必要な行を、置換可能な文字列に変更しておく。

root@mail:/opt/postfixadmin# diff -u config.inc.php.old config.inc.php     
--- config.inc.php.old  2011-01-02 21:33:13.000000000 +0000
+++ config.inc.php  2014-04-10 04:35:13.000000000 +0000
@@ -23,12 +23,12 @@
  * Doing this implies you have changed this file as required.
  * i.e. configuring database etc; specifying setup.php password etc.
  */
-$CONF['configured'] = false;
+$CONF['configured'] = true;
 
 // In order to setup Postfixadmin, you MUST specify a hashed password here.
 // To create the hash, visit setup.php in a browser and type a password into the field,
 // on submission it will be echoed out to you as a hashed value.
-$CONF['setup_password'] = 'changeme';
+$CONF['setup_password'] = '__REPLACE_POSTFIXADMIN_SETUP_PASSWORD__';
 
 // Postfix Admin Path
 // Set the location of your Postfix Admin installation here.
@@ -40,16 +40,16 @@
 
 // Language config
 // Language files are located in './languages', change as required..
-$CONF['default_language'] = 'en';
+$CONF['default_language'] = 'ja';
 
 // Database Config
 // mysql = MySQL 3.23 and 4.0, 4.1 or 5
 // mysqli = MySQL 4.1+ 
 // pgsql = PostgreSQL
 $CONF['database_type'] = 'mysql';
-$CONF['database_host'] = 'localhost';
+$CONF['database_host'] = '__REPLACE_DATABASE_HOST__:__REPLACE_DATABASE_PORT__';
 $CONF['database_user'] = 'postfix';
-$CONF['database_password'] = 'postfixadmin';
+$CONF['database_password'] = '__REPLACE_DATABASE_PASSWORD__';
 $CONF['database_name'] = 'postfix';
 // If you need to specify a different port for a MYSQL database connection, use e.g.
 //   $CONF['database_host'] = '172.30.33.66:3308';
@@ -79,7 +79,7 @@
 // Site Admin
 // Define the Site Admins email address below.
 // This will be used to send emails from to create mailboxes.
-$CONF['admin_email'] = 'postmaster@change-this-to-your.domain.tld';
+$CONF['admin_email'] = 'admin@__REPLACE_DOMAIN_NAME__';
 
 // Mail Server
 // Hostname (FQDN) of your mail server.
@@ -130,10 +130,9 @@
 // Default Aliases
 // The default aliases that need to be created for all domains.
 $CONF['default_aliases'] = array (
-    'abuse' => 'abuse@change-this-to-your.domain.tld',
-    'hostmaster' => 'hostmaster@change-this-to-your.domain.tld',
-    'postmaster' => 'postmaster@change-this-to-your.domain.tld',
-    'webmaster' => 'webmaster@change-this-to-your.domain.tld'
+    'hostmaster' => 'hostmaster@__REPLACE_DOMAIN_NAME__',
+    'postmaster' => 'postmaster@__REPLACE_DOMAIN_NAME__',
+    'webmaster' => 'webmaster@__REPLACE_DOMAIN_NAME__'
 );
 
 // Mailboxes
@@ -141,13 +140,13 @@
 // Examples:
 //   YES: /usr/local/virtual/domain.tld/username@domain.tld
 //   NO:  /usr/local/virtual/username@domain.tld
-$CONF['domain_path'] = 'NO';
+$CONF['domain_path'] = 'YES';
 // If you don't want to have the domain in your mailbox set this to 'NO'.
 // Examples: 
 //   YES: /usr/local/virtual/domain.tld/username@domain.tld
 //   NO:  /usr/local/virtual/domain.tld/username
 // Note: If $CONF['domain_path'] is set to NO, this setting will be forced to YES.
-$CONF['domain_in_mailbox'] = 'YES';
+$CONF['domain_in_mailbox'] = 'NO';
 // If you want to define your own function to generate a maildir path set this to the name of the function.
 // Notes: 
 //   - this configuration directive will override both domain_path and domain_in_mailbox
@@ -213,7 +212,7 @@
 // This is the autoreply domain that you will need to set in your Postfix
 // transport maps to handle virtual vacations. It does not need to be a
 // real domain (i.e. you don't need to setup DNS for it).
-$CONF['vacation_domain'] = 'autoreply.change-this-to-your.domain.tld';
+$CONF['vacation_domain'] = 'autoreply.__REPLACE_DOMAIN_NAME__';
 
 // Vacation Control
 // If you want users to take control of vacation set this to 'YES'.
@@ -280,14 +279,14 @@
 $CONF['header_text'] = ':: Postfix Admin ::';
 
 // link to display under 'Main' menu when logged in as a user.
-$CONF['user_footer_link'] = "http://change-this-to-your.domain.tld/main";
+$CONF['user_footer_link'] = "http://__REPLACE_FQDN__/main";
 
 // Footer
 // Below information will be on all pages.
 // If you don't want the footer information to appear set this to 'NO'.
 $CONF['show_footer_text'] = 'YES';
-$CONF['footer_text'] = 'Return to change-this-to-your.domain.tld';
-$CONF['footer_link'] = 'http://change-this-to-your.domain.tld';
+$CONF['footer_text'] = 'Return to __REPLACE_FQDN__';
+$CONF['footer_link'] = 'http://__REPLACE_FQDN__';
 
 // Welcome Message
 // This message is send to every newly created mailbox.
@@ -442,4 +441,4 @@
 //
 // END OF CONFIG FILE
 //
-/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
+/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
\ No newline at end of file

run

実際に実行する postfixadmin を実行するファイル。

#!/bin/bash
set -e

export POSTFIX_DOMAIN=`hostname --domain`
export POSTFIX_FQDN=`hostname --fqdn`

sed -i "s/__REPLACE_DATABASE_HOST__/${MYSQL_PORT_3306_TCP_ADDR}/g" /opt/postfixadmin/config.inc.php
sed -i "s/__REPLACE_DATABASE_PORT__/${MYSQL_PORT_3306_TCP_PORT}/g" /opt/postfixadmin/config.inc.php
sed -i "s/__REPLACE_DATABASE_PASSWORD__/${POSTFIX_MYSQL_PASSWORD}/g" /opt/postfixadmin/config.inc.php
sed -i "s/__REPLACE_POSTFIXADMIN_SETUP_PASSWORD__/${POSTFIXADMIN_SETUP_PASSWORD}/g" /opt/postfixadmin/config.inc.php
sed -i "s/__REPLACE_DOMAIN_NAME__/${POSTFIX_DOMAIN}/g" /opt/postfixadmin/config.inc.php
sed -i "s/__REPLACE_FQDN__/${POSTFIX_FQDN}/g" /opt/postfixadmin/config.inc.php

cd /opt/postfixadmin
/usr/bin/php -S 0.0.0.0:8080

設定ファイルを実際の値に変換した後に起動している。

docker run 時に --link オプションを利用して mysql のコンテナとリンクしておくと、 MYSQL_PORT_3306_TCP_ADDRMYSQL_PORT_3306_TCP_PORT という変数が利用可能になっているので、 データベース接続時のホスト名とポートはそこから取得するようにした。 もちろんコンテナ実行時の環境変数にその値を入れても良い。

potfixadmin の実行

実行前に MySQL を実行しているコンテナが以下のように起動している。

$ docker ps                                
CONTAINER ID        IMAGE                   COMMAND               CREATED             STATUS              PORTS                    NAMES
e8cbca190254        yuanying/mysql:latest   /usr/local/bin/run    2 seconds ago       Up 2 seconds        0.0.0.0:3306->3306/tcp   mysql

この mysql にたいして、postfix というデータベースおよび postfix ユーザを作成しておく。 パスワードはとりあえず postfixpassword にしておく。

$ mysql -h localhost -uroot -P 3306

mysql> create database postfix default character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on postfix.* to postfix@'%' identified by 'postfixpassword';
Query OK, 0 rows affected (0.00 sec)

この mysql コンテナとリンクした形で postfixadmin コンテナを起動する。

$ docker run -d --link mysql:mysql \
  -e "POSTFIX_MYSQL_PASSWORD=postfixpassword" \
  -e "POSTFIXADMIN_SETUP_PASSWORD=111111111111111111:1111111111111111111" \
  -h 'mail.fraction.jp' \
  -v /home/vmail:/var/vmail \
  -p 8080:8080 \
  yuanying/postfixadmin

POSTFIX_MYSQL_PASSWORD には先ほど設定したパスワードを。 POSTFIXADMIN_SETUP_PASSWORD には postfixadmin が生成するセットアップパスワードを設定しておく。 この値がわからなかった場合は適当に postfixadmin を構築し、setup.php から生成しておく。

http://localhost:8080/setup.php に接続し、テーブル作成および管理者を作成する。

ソース

以下に置いておく。

  • https://github.com/yuanying/docker-postfixadmin

次は postfix を構築する。