I need to set up a postfix server to send emails. After a few searches, I stumbled upon this tutorial: https://www.linuxbabe.com/mail-server/setup-basic-postfix-mail-sever-ubuntu.
Because I remember the pain it was to install and configure postfix, I thought about automating the install procedure using Docker. The goal is to test postfix locally using a dummy domain (postfix-01.mydomain.com
), and then avoid having to go through the entire procedure when going live.
Dockerfile
debconf-utils
is used to provide values for the question we're asked during a classic and interactive install of Postfix.uproute2
is for running thess
(socket stats) test after the installation is complete.entrypoint.sh
is used to run postfix as a service (/etc/init.d/postfix start
)
FROM ubuntu:22.04 AS builder
# update apt
# install debconf-utils for pre-configuration (-y says yes or default to all questions during install)
# install iproute2 for running ss (socket stats)
RUN apt-get update && \
apt-get install -y debconf-utils && \
apt-get install -y iproute2
# Pre-configure Postfix with debconf-set-selections
# see https://gist.github.com/gene1wood/e4dd448513cb425b5ec398f95cda2462
RUN echo "postfix postfix/main_mailer_type select internet_site" | debconf-set-selections
RUN echo "postfix postfix/mydomain string mydomain.com" | debconf-set-selections
RUN echo "postfix postfix/mailname string postfix-01.mydomain.com" | debconf-set-selections
RUN echo "postfix postfix/root_address string [email protected]" | debconf-set-selections
# Install postfix with pre-populated selections
RUN apt-get install -y postfix
EXPOSE 25 465
# COPY configuration file
COPY conf/main.cf /etc/postfix/main.cf
# Entrypoint script to start Postfix
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
#!/bin/bash
# Start Postfix service
/etc/init.d/postfix start
# Keep the container running
tail -f /dev/null
main.cf
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 3.6 on
# fresh installs.
compatibility_level = 3.6
# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=may
smtp_tls_CApath=/etc/ssl/certs
smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = postfix-01.mydomain.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = postfix-01.mydomain.com, $myhostname, localhost, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
maillog_file = /dev/stdout
Build Command
docker build -t postfix:ubuntu.22.04 --no-cache --progress=plain .
Run Command
docker run -d --name postfix-ubuntu.22.04 --hostname postfix-01.mydomain.com -p 25:25 -p 465:465 postfix:ubuntu.22.04
Problem: sendmail doesn't work and there's no log
echo "test email" | sendmail [email protected]
Tests & Checks
- Postfix is running
# postfix status
postfix/postfix-script: the Postfix mail system is running: PID: 583
- Socket Stats shows empty values in the process column
ss -lnpt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 100 0.0.0.0:25 0.0.0.0:*
LISTEN 0 100 [::]:25 [::]:*
master.cf shows that logs should be output to
stdout`
postlog unix-dgram n - n - 1 postlogd