删除这些行:
RUN touch /dev/xconsole && chgrp syslog /dev/xconsole && chmod 664 /dev/xconsole
RUN service rsyslog restart
并首先更新CMD 以启动rsyslog:
CMD service rsyslog start && service postfix start && tail -f /var/log/mail.log
因为RUN 在镜像构建期间执行命令,而CMD 在运行容器中执行命令,您需要在运行容器中运行 rsyslogd。
结果 Dockerfile 可能如下所示:
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
构建它:docker build -t postfix -f Dockerfile.postfix .
当你运行它时:docker run -it --rm --name postfix postfix
...然后你有邮件登录控制台:
* 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
您还可以使用 exec 命令检查邮件日志:docker exec postfix cat /var/log/mail.log。这在将容器作为守护进程运行时会很有帮助(-d 标志)。
关于rsyslogd-2039: Could not open output pipe 错误 - 您可以忽略它。
但如果你不想忽略它,你可以更新你的CMD 并像这样:
CMD touch /dev/xconsole && chgrp syslog /dev/xconsole && chmod 664 /dev/xconsole && service rsyslog start && service postfix start && tail -f /var/log/mail.log