Search code examples
dockerdockerfilepostfix-mta

Dockerizing Postfix Relay server


I'm trying to build a docker container dedicated to a Postfix SMTP relay. I fail to make it start the postfix service on it after several tries.

Here's the dockerfile

FROM centos:centos7
LABEL Author = "Aurelien HUGON 
LABEL Description = "DOCKERFILE : Creates a Docker Container for a Relay Postfix smtp server"

#Update and soft
RUN yum update -y

RUN yum install -y nano postfix

#Clean install
RUN yum clean all

#Config
COPY /cfg/config.sh /
RUN chmod +x config.sh
RUN ./config.sh
RUN touch /var/log/maillog

CMD ["sh", "-c", "/sbin/postfix start", "tail -f /var/log/maillog"]

The config.sh file contains :

postconf -e 'myhostname = myserverhostname'
postconf -e 'mydomain = domain.com'
postconf -e 'myorigin = $mydomain'
postconf -e 'smtp_generic_maps = hash:/etc/postfix/generic'
postconf -e 'mynetworks = 127.0.0.1/32 192.168.0.0/16 172.16.0.0/12 10.0.0.0/8 [::1]'
postconf -e 'mynetworks_style = class'
postconf -e 'inet_interfaces = all'
postconf -e 'relayhost = [dedicated SMTP server]'
echo "generic@adress.com generic2@adress2.com" > /etc/postfix/generic
postmap /etc/postfix/generic

I tried to use the "postfix start" command as entrypoint in my dockerfile but the container instantly shuts down with return code 0. I tried to start the container with the CMD by removing the /sbin/postfix start part, my container starts and is stable, but i have to start the postfix service manually. And then my relay works. But that's suboptimal.

I found solutions using supervisord, but i want to keep my container as simple as possible. My goal is to have a light interchangeable relay to send my mails from my application hosted on the docker server.


Solution

  • I finally decided to cheat and use Supervisord.