12

Is there any way to run Postfix in foreground?

I want to run Postfix as PID 1 in docker. I don't want to use any bash shell, supervisorD, or any wrapper to start it.

I just want to know if there is any way to start it in foreground so I can check its logs from "docker logs 'container-name'"

When I manually run "postfix start" it runs and then starts in background. Any way to keep it in foreground?

Thanks

5 Answers 5

6

Postfix needs a syslog daemon for logging. For a concise example of a Dockerfile running Postfix see jessfraz/dockerfiles/postfix.

This runs rsyslog in the container and starts Postfix parallel to that like this:

exec /usr/lib/postfix/master -c /etc/postfix -d 2>&1
tail -F /var/log/mail.log
2
  • 2
    You don't need a syslog daemon inside the container. You just need a syslog service reading from /dev/log. You can provide this with -v /dev/log:/dev/log when running your container. With this, postifx's logs will be in the host's journal.
    – starfry
    Jun 15, 2020 at 10:45
  • 2
    Since Postfix 3.4 having to provide a syslog service isn't required anymore, you can use stdout since Postfix 3.4.0
    – FibreFoX
    Jul 11, 2020 at 21:29
24

Starting with Postfix version 3.3, you can also use postfix start-fg, which according to the documentation:

Like start, but keep the master(8) daemon running in the fore-ground

It makes things much easier!

2
  • Please note that if Postfix is launched in chroot environment (which is default in many distros) with postfix start-fg, you will have the /var/spool/postfix/etc dir empty, postfix will not know your localtime, hosts, resolv.conf, you will not be able to deliver a single message. See the difference when you run regular service postfix start.
    – sekrett
    Sep 26, 2018 at 15:22
  • 3
    @sekrett That is correct, however, while inside a Docker container as per the OP's question, I would advise not to use chroot (it would be redundant). Jan 4, 2019 at 14:00
11

Since Postfix 3.3, docker is natively supported. Simply build and run this Dockerfile :

FROM alpine:3

RUN apk add --no-cache postfix postfix-pcre
RUN echo "maillog_file = /dev/stdout" >> /etc/postfix/main.cf

CMD ["/usr/sbin/postfix","start-fg"]
3

If I understood your question you want to run postfix in the foregroud, so it gets its logs sent to the stdout and you can display them using docker logs.

You cannot run Postfix in a way that its logs are sent to the stdout. From the official docs "Postfix daemon processes run in the background, and log problems and normal activity to the syslog daemon.", so syslogd is used by Postfix to manage its logs, and that's a requirement you cannot avoid.

As Docker will "ignore" log output that is not sent to stdout/stderr you should have to look for alternatives.

This conversation gives you more information about postfix and syslogd requirement and possible strategies in Docker. Basically it will try to send the syslog output to your host's syslog log files, but you won't be able to use docker logs to read them.

This old container in Docker Hub tries to do what you want, at least explains the same issue that you want to solve.

Finally, I found this article helpful (referenced in the nabble discussion) to understand the problem of getting the syslogd information out of a container.

3

Since Postfix 3.4 this is supported properly, elimitating a syslog dependency.

Excerpt from postfix/MAILLOG_README:

Configuring logging to stdout

Logging to stdout is useful when Postfix runs in a container, as it eliminates a syslogd dependency.

  1. Add the following line to master.cf if not already present (note: there must be no whitespace at the start of the line):

    postlog   unix-dgram n  -       n       -       1       postlogd
    

    Note: the service type unix-dgram was introduced with Postfix 3.4. Remove the above line before backing out to an older Postfix version.

  2. Configure main.cf with maillog_file = /dev/stdout.

  3. Start Postfix with postfix start-fg.

My Docker entrypoint for starting Postfix leverages postconf to patch the configuration as described:

#!/bin/bash -e

postconf maillog_file=/dev/stdout

# Rely on the Postfix 3.4+ default master.cf containing the line
# 'postlog   unix-dgram [...]'

exec /usr/sbin/postfix start-fg "$@"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .