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 smtp.office365.com:587

start.sh 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

#!/bin/bash

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

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

Docker file

FROM: ubuntu:latest

WORKDIR /home

COPY . .

RUN echo "postfix postfix/mailname string rundeck.example.com" | 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 "[smtp.office365.com]:587 svc-user@example.com:password" > /etc/postfix/sasl_passwd && echo "/.+/ ssvc-user@example.com" > /etc/postfix/sender_canonical && sed -i 's/inet_protocols = all/inet_protocols = ipv4/g' /etc/postfix/main.cf && sed -i 's/relayhost = /relayhost = [smtp.office365.com]:587/g' /etc/postfix/main.cf && sed -i '/smtpd_use_tls=yes/a smtp_sasl_auth_enable = yes' /etc/postfix/main.cf && sed -i '/smtpd_use_tls=yes/a smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd' /etc/postfix/main.cf && sed -i '/smtpd_use_tls=yes/a smtp_sasl_security_options = noanonymous' /etc/postfix/main.cf && sed -i '/smtpd_use_tls=yes/a smtp_tls_security_level = may' /etc/postfix/main.cf && sed -i '/smtpd_use_tls=yes/a sender_canonical_maps = regexp:/etc/postfix/sender_canonical' /etc/postfix/main.cf && sed -i '/smtpd_use_tls=yes/a smtp_tls_CAfile = /etc/postfix/cacert.pem' /etc/postfix/main.cf && sed -i 's#128#& 172.17.0.0/16#' /etc/postfix/main.cf && mv /home/cacert.pem /etc/postfix/ && postmap hash:/etc/postfix/sasl_passwd && postmap hash:/etc/postfix/sender_canonical

ENTRYPOINT ["./start.sh"]

Put all 3 files in same directory and run

docker build . -t some_tag

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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