I AM NOT A SERVER ADMINISTRATOR! In case that wasn’t clear, I am not a server administrator. What little I know about servers is enough to allow me to build one in order to my job. Everything is guessing and Googling. Recently, I had a requirement to send emails from a Docker container. Guess how many official Postfix Docker images there are. If you guessed more than zero, you were wrong. Also, you really should have seen that coming.

So I needed a Postfix image to act as a relay SMTP server for my application that was also in a Docker container. After more trial and error than I would like to admit, I got it to work. And then it stopped working. The thing is, I got it to work as a daemon. I then used the start-fg command line argument to start it in the foreground in order to keep the container running. Great idea. Almost.

When it ran as a daemon, it worked perfectly, but I couldn’t use it as a daemon because I needed it to run in the foreground. When it ran in the foreground, it gave me DNS errors. Why would DNS only work as a daemon? I spend way too much time looking for that answer.

The daemon created links or copied certain files from /etc to /var/spool/postfix/etc. The start-fg command line argument did not. It wasn’t something that I would just notice. It was something that I stumbled upon accidentally. Somewhere in the land of stack overflow, I found a hint. That hint led to an idea. That idea failed a horrible death.

The next attempt was to run as a service again and look at the contents of the /var/spool/postfix/etc directory. I then copied each of the files in it from /etc and like magic, my new container worked.

Here is the Dockerfile I used.

FROM ubuntu:18.04


RUN apt-get update && \
  echo "postfix postfix/mailname string example.com" | debconf-set-selections && \
  echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections && \
  apt-get install postfix mailutils -y

RUN update-rc.d -f postfix remove

RUN postconf -e syslog_name=example-smtp
RUN postconf -e mynetworks=

RUN cp /etc/host.conf /etc/hosts /etc/nsswitch.conf /etc/resolv.conf /etc/services /var/spool/postfix/etc

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

In order for you to use it, swap “example” with your domain. Enjoy.