-1

I have implemented a dockerfile to have a postfix that logs using rsyslog, but it raises an error while running.

Output Error:

tail: cannot open '/var/log/mail.log' for reading: No such file or directory
tail: no files remaining

Here is my Dockerfile:

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y libsasl2-modules postfix rsyslog && rm -rf /var/lib/apt/lists/*

COPY main.cf /etc/postfix/
COPY ca-certificates.crt /etc/ssl/certs/

EXPOSE 25

CMD service rsyslog start && service postfix start && tail -f /var/log/mail.log

Any help will be greatly appreciated.

MWA
  • 51
  • 2
  • 11

1 Answers1

0

Remove these lines:

RUN touch /dev/xconsole && chgrp syslog /dev/xconsole && chmod 664 /dev/xconsole
RUN service rsyslog restart

And update CMD to start rsyslog at first:

CMD service rsyslog start && service postfix start && tail -f /var/log/mail.log

Because RUN executes command during the image build whereas CMD executes command in the running container and you need to run rsyslogd in a running container.

Result Dockerfile can look like this:

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y libsasl2-modules postfix rsyslog; \
    rm -rf /var/lib/apt/lists/*
RUN echo "[smtp.gmail.com]:587 email@gmail.com:password" > /etc/postfix/sasl_passwd; \
    postmap /etc/postfix/sasl_passwd; \
    sed -i '/relayhost*/c\relayhost = [smtp.gmail.com]:587' /etc/postfix/main.cf; \
    sed -i '/smtp_sasl_auth_enable*/c\smtp_sasl_auth_enable = yes' /etc/postfix/main.cf; \
    sed -i '/smtp_sasl_security_options*/c\smtp_sasl_security_options = noanonymous' /etc/postfix/main.cf; \
    sed -i '/smtp_sasl_password_maps*/c\smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd' /etc/postfix/main.cf; \
    sed -i '/smtp_tls_security_level*/c\smtp_tls_security_level = encrypt' /etc/postfix/main.cf; \
    sed -i '/smtp_tls_CAfile*/c\smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt' /etc/postfix/main.cf

EXPOSE 587

CMD service rsyslog start && service postfix start && tail -f /var/log/mail.log

Build it: docker build -t postfix -f Dockerfile.postfix .

And when you run it: docker run -it --rm --name postfix postfix

... then you have mail log in console:

 * Starting enhanced syslogd rsyslogd                                    [ OK ] 
 * Starting Postfix Mail Transport Agent postfix                                                                                                                                                                   postfix: Postfix is running with backwards-compatible default settings
postfix: See http://www.postfix.org/COMPATIBILITY_README.html for details
postfix: To disable backwards compatibility use "postconf compatibility_level=2" and "postfix reload"
                                                                                                                                                                                                            [ OK ]
Sep 26 20:11:01 6009a2e3d206 postfix[111]: Postfix is running with backwards-compatible default settings
Sep 26 20:11:01 6009a2e3d206 postfix[111]: See http://www.postfix.org/COMPATIBILITY_README.html for details
Sep 26 20:11:01 6009a2e3d206 postfix[111]: To disable backwards compatibility use "postconf compatibility_level=2" and "postfix reload"
Sep 26 20:11:01 6009a2e3d206 postfix/master[156]: daemon started -- version 3.1.0, configuration /etc/postfix
Sep 26 20:11:01 6009a2e3d206 postfix/pickup[159]: error: open file /etc/mailname: No such file or directory
Sep 26 20:11:01 6009a2e3d206 postfix/qmgr[160]: error: open file /etc/mailname: No such file or directory

You can also check the mail log using exec command: docker exec postfix cat /var/log/mail.log. This can be helpful when running container as a daemon (-d flag).


Regarding the rsyslogd-2039: Could not open output pipe error - you can ignore it.

But if you don't want to ignore it you can update your CMD and have it like this:

CMD touch /dev/xconsole && chgrp syslog /dev/xconsole && chmod 664 /dev/xconsole && service rsyslog start && service postfix start && tail -f /var/log/mail.log
cgrim
  • 4,890
  • 1
  • 23
  • 42
  • everything works fine except when i add COPY / ADD before EXPOSE command like this (COPY main.cf /etc/postfix/), it results to `tail: cannot open '/var/log/mail.log' for reading: No such file or directory` – MWA Oct 03 '18 at 15:44
  • I tried that and it _works_. It works also with incorrect _main.cf_ file and shows `* Starting enhanced syslogd rsyslogd [ OK ] * Starting Postfix Mail Transport Agent postfix postmulti: fatal: /etc/postfix/main.cf, line 1: missing '=' after attribute name: "test"` Postfix shows error because of incorrect config file nevertheless the rsyslog is running and _/var/log/mail.log_ file was created and container runs without exit. From my point of view you have problem somewhere else. Did you try it with exactly the same _Dockerfile_ as is in the answer? – cgrim Oct 05 '18 at 12:46
  • I have no idea what's wrong with this rsyslog thing, there must be a solution to it, coz it just randomly worked fine and sometimes it doesn't, I really couldn't tell why. But here is my final and simple Dockerfile but still the problem is there. Also, whenever I tried to update it, it just raises an error. – MWA Oct 05 '18 at 14:49