Docker – create Postfix container

Posted: February 21, 2019 in docker, Linux

In this post we’ll create Docker image with postfix installed and configured as Office 365 relay host, pip3 will be also installes as well as Python request.

cacert.pem file contains Microsoft certificates for secure connection. Content of that file is output of following command:

openssl s_client -showcerts -starttls smtp -crlf -connect is simple bash script which starts postfix and saslauthd services and sends test email. In order to prevent closing docker container before sending email i added sleep command at the end


service postfix start && service saslauthd start
echo "sending email..."
echo "this is the body" | mail -s "this is the subject" ""
sleep 20

requirements.txt contains one string: requests, it will be used by pip3 command

Docker file

FROM: ubuntu:latest


COPY . .

RUN echo "postfix postfix/mailname string" | debconf-set-selections && echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections && apt-get update -y && apt-get install postfix sasl2-bin mailutils python3-pip -y && pip3 install --no-cache-dir -r requirements.txt && sed -i s/START=no/START=yes/g /etc/default/saslauthd && echo "[]:587" > /etc/postfix/sasl_passwd && echo "/.+/" > /etc/postfix/sender_canonical && sed -i 's/inet_protocols = all/inet_protocols = ipv4/g' /etc/postfix/ && sed -i 's/relayhost = /relayhost = []:587/g' /etc/postfix/ && sed -i '/smtpd_use_tls=yes/a smtp_sasl_auth_enable = yes' /etc/postfix/ && sed -i '/smtpd_use_tls=yes/a smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd' /etc/postfix/ && sed -i '/smtpd_use_tls=yes/a smtp_sasl_security_options = noanonymous' /etc/postfix/ && sed -i '/smtpd_use_tls=yes/a smtp_tls_security_level = may' /etc/postfix/ && sed -i '/smtpd_use_tls=yes/a sender_canonical_maps = regexp:/etc/postfix/sender_canonical' /etc/postfix/ && sed -i '/smtpd_use_tls=yes/a smtp_tls_CAfile = /etc/postfix/cacert.pem' /etc/postfix/ && sed -i 's#128#&' /etc/postfix/ && mv /home/cacert.pem /etc/postfix/ && postmap hash:/etc/postfix/sasl_passwd && postmap hash:/etc/postfix/sender_canonical


Put all 3 files in same directory and run

docker build . -t some_tag


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s