Dockerでメールサーバを建てる

Dockerのよさが全く活かせてない記事なので,読まないことを強く推奨します.
ただのメモです.

やっとの思いでメールサーバが建ったので,まとめておく
(ほとんど参考先のコピペorz)

忘れやすいもんだから,この記事もどっか抜けてる可能性ある
ちなみに,セキュリティとか「なにそれ,おいしいの」状態

参考

Docker以前にCentOSでメールサーバ建てられないとどうしようもないわけです

CentOSでメールサーバ建てる手順(適当)

行番号とかは,参考程度に

  1. とりあえずおもむろにインストール

    yum -y install postfix
  2. 設定をごにょごにょ

    vi /etc/postfix/main.cf
    • myhostname = メールドメイン名(L.78)
    • mydomain = メインドメイン(L.83)
    • myorigin = $mydomain(L.99)
    • inet_interfaces = all(L.116)
    • home_mailbox = Maildir/(L.419)
    • smtpd_banner = $myhostname ESMTP unknown(L.569)
    • 末尾に以下を追記
      smtpd_sasl_auth_enable = yes
      smtpd_sasl_type = dovecot
      smtpd_sasl_path = private/auth
      smtpd_sasl_security_options = noanonymous
      smtpd_sasl_local_domain = $myhostname
      smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
      mailbox_size_limit = 1073741824
    vi /etc/postfix/master.cf
    • 12行目付近を以下のようにする
      submission inet n       -       n       -       -       smtpd
      
      #  -o smtpd_tls_security_level=encrypt
      
        -o smtpd_sasl_type=dovecot
        -o smtpd_sasl_path=private/auth
        -o smtpd_sasl_security_options=noanonymous
        -o smtpd_sasl_local_domain=$myhostname
        -o smtpd_sasl_auth_enable=yes
        -o smtpd_client_restrictions=permit_sasl_authenticated,reject
        -o milter_macro_daemon_name=ORIGINATING
  3. おもむろにインストール,その2

    yum -y install dovecot
  4. 設定をごにょごにょ,その2

    vi /etc/dovecot/conf.d/10-mail.conf
    • mail_location = maildir:~/Maildir(L.30)
    vi /etc/dovecot/conf.d/10-auth.conf
    • disable_plaintext_auth = no(L.9)
    • auth_mechanisms = plain login(L.97)
    vi /etc/dovecot/conf.d/10-master.conf
    • 88行目以降を以下のようにする
      unix_listener /var/spool/postfix/private/auth { 
       mode = 0666 
       user = postfix 
       group = postfix 
      }
  5. Maildirを作成

    mkdir -p /etc/skel/Maildir/{new,cur,tmp}
    chmod -R 700 /etc/skel/Maildir/

    ここでDockerだったら,Commitしておく
    そのあと,110番(POP3),143番(IMAP),587番(SMTP/Submission)をポートあててrunする

  6. ユーザを追加

    useradd -s /sbin/nologin UserName
    passwd UserName
  7. おもむろに起動してやる

    /etc/init.d/postfix start
    /etc/init.d/saslauthd start
    /etc/init.d/dovecot start
  8. お疲れ様でした

Dockerでメールサーバを構築する(適当)

参考


  1. centosのイメージを手に入れておく

    docker pull coreos:coreos6
  2. Dockerfileを適当なディレクトリにつくる

    FROM centos:centos6
    RUN yum -y install initscripts MAKEDEV
    RUN yum check
    RUN yum -y update
    
    RUN yum -y install openssh-server
    
    RUN sed -ri 's/^#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config
    RUN sed -ri 's/^UsePAM yes/UsePAM no/' /etc/ssh/sshd_config
    
    RUN /etc/init.d/sshd start
    
    
    # 以下の{password}を任意のパスワードにする
    
    RUN echo 'root:{password}' | chpasswd
    
    EXPOSE 22
    
    CMD /sbin/init
  3. コンテナイメージを作る

    docker build -t ssh-enabled .

    ここで生成されたイメージファイルは何かと便利

  4. メールサーバ建てるようにコンテナをつくる

    docker run -P -d ssh-enabled
  5. SSHのポートを確認しておく

    docker ps -a # もっといいやり方あると思う
  6. SSHを繋げる

    alias ssh-ignorekey='ssh -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no'
    ssh-ignorekey -p {ssh_port} root@{serverip}
  7. メールサーバ建てる手順の5までを行う

  8. supervisorをインストールする

    yum install python-setuptools -y
    easy_install supervisor
  9. supervisorの起動スクリプトをつくる

    vi /etc/init.d/supervisord
    
    #!/bin/sh
    
    #
    
    # /etc/rc.d/init.d/supervisord
    
    #
    
    # Supervisor is a client/server system that
    
    
    # allows its users to monitor and control a
    
    
    # number of processes on UNIX-like operating
    
    
    # systems.
    
    #
    
    # chkconfig: - 64 36
    
    
    # description: Supervisor Server
    
    
    # processname: supervisord
    
    
    
    # Source init functions
    
    . /etc/init.d/functions
    
    RETVAL=0
    prog="supervisord"
    pidfile="/tmp/supervisord.pid"
    lockfile="/var/lock/subsys/supervisord"
    
    start()
    {
       echo -n $"Starting $prog: "
       daemon --pidfile $pidfile supervisord
       RETVAL=$?
       echo
       [ $RETVAL -eq 0 ] && touch ${lockfile}
    }
    stop()
    {
       echo -n $"Shutting down $prog: "
       killproc -p ${pidfile} /usr/bin/supervisord
       RETVAL=$?
       echo
       if [ $RETVAL -eq 0 ] ; then
          rm -f ${lockfile} ${pidfile}
       fi
    }
    case "$1" in
      start)
        start
      ;;
      stop)
        stop
      ;;
      status)
        status $prog
      ;;
      restart)
        stop
        start
      ;;
      *)
        echo "Usage: $0 {start|stop|restart|status}"
      ;;
    esac
    chmod 755 /etc/init.d/supervisord
  10. supervisorの設定ファイルを作成

    echo_supervisord_conf > /etc/supervisord.conf
    echo "[include]" >> /etc/supervisord.conf
    echo "files = supervisord/conf/*.conf" >> /etc/supervisord.conf
    mkdir -p /etc/supervisord/conf/
    vi /etc/supervisord/conf/service.conf
    [supervisord]
    nodaemon=true
    
    [program:sshd]
    command=/usr/sbin/sshd -D
    autostart=true
    autorestart=true
    
    [program:saslauthd]
    command=/etc/init.d/saslauthd start
    autostart=true
    
    [program:postfix]
    command=/etc/init.d/postfix start
    autostart=true
    
    [program:dovecot]
    command=/etc/init.d/dovecot start
    autostart=true
  11. おもむろにCommit

    docker commit mail mailserver
  12. ポートを開けたコンテナに立て直し

    docker stop mail
    docker rm mail
    docker run -p 22 -p 110:110 -p 143:143 -p 587:587 -d --name="mail" mailserver /usr/bin/supervisord
  13. SSHで接続して,ユーザ追加

    docker ps -a
    
    alias ssh-ignorekey='ssh -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no'
    ssh-ignorekey -p {ssh_port} root@{serverip}
    useradd -s /sbin/nologin UserName
    passwd UserName
  14. お疲れ様でした