14

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

Dave M
  • 4,494
Farhan
  • 4,377
  • 12
  • 56
  • 87

5 Answers5

27

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!

12

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"]

7

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
webwurst
  • 422
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 "$@"

gertvdijk
  • 3,664
  • 6
  • 32
  • 51
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.

Miguel A. C.
  • 1,526