Postfix 的 Dockerfile

Postfix 的 Dockerfile

我曾尝试在 dockerfile 中实现 postfix,但当我尝试发送邮件时失败,提示“地址解析失败”。我需要重新配置 docker 网络还是仅重新配置 postfix 主配置?

发送电子邮件:

sendmail [email protected]
From: [email protected]
Subject: theSubject
This is the message
.

输出错误:

postfix/error[573]: 322CC760259: to=<NEXTRCPT NOT UPDATED>, orig_to=<[email protected]>, relay=none, delay=15, delays=15/0/0/0.1, dsn=4.3.0, status=deferred (address resolver failure)

postfix 主配置(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 2 on
# fresh installs.
compatibility_level = 2

# 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_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = meron
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = $myhostname, meron, localhost.localdomain, , localhost
relayhost = [smtp.gmail.com]:587 
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

# Enable SASL authentication
smtp_sasl_auth_enable = yes
# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passd
# Enable STARTTLS encryption
smtp_tls_security_level = encrypt
# Location of CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Dockerfile:

FROM ubuntu:16.04

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

CMD touch /dev/xconsole && chgrp syslog /dev/xconsole && chmod 664 /dev/xconsole

# RUN service rsyslog restart

RUN echo "[smtp.gmail.com]:587 [email protected]:password" > /etc/postfix/sasl_passwd

RUN postmap /etc/postfix/sasl_passwd

RUN sed -i '/relayhost*/c\relayhost = [smtp.gmail.com]:587' /etc/postfix/main.cf

RUN sed -i '/smtp_sasl_auth_enable*/c\smtp_sasl_auth_enable = yes' /etc/postfix/main.cf

RUN sed -i '/smtp_sasl_security_options*/c\smtp_sasl_security_options = noanonymous' /etc/postfix/main.cf

RUN sed -i '/smtp_sasl_password_maps*/c\smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd' /etc/postfix/main.cf

RUN sed -i '/smtp_tls_security_level*/c\smtp_tls_security_level = encrypt' /etc/postfix/main.cf

RUN sed -i '/smtp_tls_CAfile*/c\smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt' /etc/postfix/main.cf

# CMD /etc/init.d/postfix restart && tail -f /var/log/syslog

EXPOSE 25

# CMD service rsyslog start && service postfix start && tail -f /var/log/mail.log
CMD touch /dev/xconsole && chgrp syslog /dev/xconsole && chmod 664 /dev/xconsole && service rsyslog start && service postfix start && tail -f /var/log/mail.log

任何帮助将不胜感激。

答案1

您可以将容器从 docker bridge 网络中移除,而是通过添加--network="host"选项来使用主机的网络,docker run .....而不是使用参数映射端口-p

对于 docker-compose,您可以添加&network_mode: "host"来代替该特定服务。ports:networks:

例如,当我在本地/开发环境中运行 PHP 容器时。

Docker 命令行

docker run --rm --network="host" -v $(pwd):/app MyVendor/phpfpm71 my_command

Docker 组成:

phpfpm71:
image: MyVendor/phpfpm71
volumes:
    - ./app:/app
network_mode: "host"

答案2

我已将网关 IP 地址添加到 main.cf 中的 mydestination 配置中。

工作得很好。;)

相关内容