Postfix in a container

I want to run postfix in a container as a mail relay for my network. Here are the problems I ran in to and how I solved them.

I want to run postfix in a container as a mail relay for my network. Here are the problems I ran in to and how I solved them.

I had a couple of requirements, run the latest postfix and store the configuration outside of the container so it would be easy to run in my Kubernetes cluster.

My Dockerfile was a simple one, based on Debian 10.8. Ran apt update, apt upgrade -y and apt install -y postfix then set the container to run postfix in the foreground.

Here it is:

FROM debian:10.8

RUN apt update && \
    apt upgrade -y && \
    apt install -y postfix

RUN mkdir /config && \
    mv /etc/postfix/main.cf /config/main.cf && \
    mv /etc/postfix/master.cf /config/master.cf && \
    ln -s /config/main.cf /etc/postfix/main.cf && \
    ln -s /config/master.cf /etc/postfix/master.cf

CMD ["postfix", "start-fg"]

You may notice that I moved main.cf and master.cf into a /config directory. This is so I can mount a config map from Kubernetes into the container without blowing up the rest of /etc/postfix.

Next, you want to get it to log to the console, in your main.cf put in this line:

maillog_file=/dev/stdout

I created my image and pushed it to my private registry. And now postfix started in a container.

I wanted it to be a relay for everything on my network so I built my configuration to do the relay but it didn't work. I was getting this cryptic message:

smtp postfix/qmgr[105]: warning: private/relay socket: malformed response
smtp postfix/master[1]: warning: process /usr/lib/postfix/sbin/smtp pid 111 exit status 1
smtp postfix/master[1]: warning: /usr/lib/postfix/sbin/smtp: bad command startup -- throttling
smtp postfix/qmgr[105]: warning: transport relay failure -- see a previous warning/fatal/panic logfile record for the problem description```

With that I started ramping up the logging. In the master.cf file I started my troubleshooting by adding -v to qmgr hoping to find something there. Not much, but some additional detail.

smtp postfix/qmgr[105]: warning: private/relay socket: malformed response
smtp postfix/master[1]: warning: process /usr/lib/postfix/sbin/smtp pid 111 exit status 1
smtp postfix/master[1]: warning: /usr/lib/postfix/sbin/smtp: bad command startup -- throttling
smtp postfix/qmgr[105]: qmgr_transport_throttle: transport relay: status: 4.3.0 reason: unknown mail transport error
smtp postfix/qmgr[105]: warning: transport relay failure -- see a previous warning/fatal/panic logfile record for the problem description
smtp postfix/qmgr[105]: defer transport relay: 4.3.0 unknown mail transport error
smtp postfix/qmgr[105]: defer site example-com.mail.protection.outlook.com: 4.3.0 unknown mail transport error

There wasn't anything that helped, just thought it was interesting that I'm now seeing a new error. Next I removed the -v from qmgr and added -v to the smtp entries. That yielded an error that helped out, I started seeing this:

fatal: unknown service: smtp/tcp

After some hunting, I saw some places that said that we need to copy the /etc/services into /var/spool/postfix/etc. So I did that. Then I started getting these next errors (some lines are omitted).

smtp postfix/relay/smtp[118]: dns_query: example-com.mail.protection.outlook.com (MX): Host not found, try again
.
.
.
smtp postfix/relay/smtp[118]: send attr reason = Host or domain name not found. Name service error for name=example-com.mail.protection.outlook.com type=MX: Host not found, try again
.
.
.
smtp postfix/relay/smtp[118]: CDEFD2D83: to=<xxx@example.com>, relay=none, delay=0.03, delays=0.02/0.01/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=example-com.mail.protection.outlook.com type=MX: Host not found, try again)
.
.
.
smtp postfix/relay/smtp[118]: deliver_request_final: send: "Host or domain name not found. Name service error for name=example-com.mail.protection.outlook.com type=MX: Host not found, try again" -1
.
.
.
smtp postfix/relay/smtp[118]: send attr reason = Host or domain name not found. Name service error for name=example-com.mail.protection.outlook.com type=MX: Host not found, try again

These messages tell me that it cannot resolve my relay address. I then copied my /etc/resolv.conf into that same directory, /var/spool/postfix/etc. And suddenly it all worked. I did not like this option, it would mean needing a startup script in my container and thus increasing complexity and maintenance. After thinking about it, it seemed like it was being chrooted into /var/spool/postfix. After some digging in to how to chroot postfix processes I found that you do it in the master.cf. All I needed to do was change the chroot field of the smtp command entries to n, restart my container, and finally everything worked.

Configuration files

I have documented my config files below. These are very minimal and work as a simple relay in a Debian container with no frills, no authentication, no SSL, etc.

main.cf:

compatibility_level = 2
queue_directory = /var/spool/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
maillog_file = /dev/stdout
mynetworks = 127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
relayhost = example-com.mail.protection.outlook.com
myhostname = relay.example.com
setgid_group = postdrop
inet_protocols = ipv4
mail_name = My mail relay

master.cf:

smtp      inet  n       -       y       -       -       smtpd
pickup    unix  n       -       y       60      1       pickup
cleanup   unix  n       -       y       -       0       cleanup
qmgr      unix  n       -       n       300     1       qmgr
#qmgr     unix  n       -       n       300     1       oqmgr
tlsmgr    unix  -       -       y       1000?   1       tlsmgr
rewrite   unix  -       -       y       -       -       trivial-rewrite
bounce    unix  -       -       y       -       0       bounce
defer     unix  -       -       y       -       0       bounce
trace     unix  -       -       y       -       0       bounce
verify    unix  -       -       y       -       1       verify
flush     unix  n       -       y       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       n       -       -       smtp
relay     unix  -       -       n       -       -       smtp
        -o syslog_name=postfix/$service_name
showq     unix  n       -       y       -       -       showq
error     unix  -       -       y       -       -       error
retry     unix  -       -       y       -       -       error
discard   unix  -       -       y       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       y       -       -       lmtp
anvil     unix  -       -       y       -       1       anvil
scache    unix  -       -       y       -       1       scache
postlog   unix-dgram n  -       n       -       1       postlogd
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix  -       n       n       -       2       pipe
  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
  ${nexthop} ${user}

Conclusion

It took some time to figure out everything I needed to do to get the relay working. Now that it is, I'm happy with it, I think it will suit my needs very well. Now to move it in to my Kubernetes cluster.