问题描述
我有一个运行php和apache的。主机位于运行Docker实例的AWS实例中。我无法从码头终端发送电子邮件。有没有办法从使用docker主机配置的sendmail发送docker实例的电子邮件?
I have a docker container running php and apache. The host is in an AWS instance which has the docker instance running. I am unable to send an email from the docker terminal. Is there any way to send an email from docker instance using sendmail which uses the docker's host's configuration?
以下命令从主机发送电子邮件,但不发送电子邮件从码头实例。没有任何错误。
The following command sends an email from host but doesn't send an email from docker instance. No error is given either.
echo "Subject: Testing Email" | cat - text | /usr/lib/sendmail -F [email protected] -t [email protected]
推荐答案
我所做的是配置主机MTA来收听 docker0
,并在容器中安装ssmtp以将sendmail挂载在容器与主机MTA。在主机上运行MTA的原因是系统(关键)错误可以发送到管理员的邮箱。在容器中不运行MTA的原因是主机系统已经运行MTA,这是一个重复的过程。
What I do is to configure the host MTA to listen on docker0
and install ssmtp in the container to bridge sendmail in the container with the host MTA. The reason to run an MTA on the host is that system (critical) errors can be sent to the admin's mailbox. The reason to not run MTA in the container is that it is a duplicated process as the host system already runs an MTA.
在主机上,我使用了后缀。我们所需要做的就是配置postfix来收听 docker0
并接受Docker容器发送的邮件。编辑文件 /etc/postfix/main.cf
并将 docker0
IP地址添加到 inet_interfaces
,所以它接受来自Docker容器的连接。另外,将Docker容器的网络地址添加到 mynetworks
,以便Docker容器通过主机上的postfix服务器发送邮件是合法的。 ()
On the host, I used postfix. All we need to do is to configure postfix to listen on docker0
and accept outgoing mails from Docker containers. Edit the file /etc/postfix/main.cf
and add the docker0
IP address to inet_interfaces
so it accepts connections from Docker containers. Also, add the network addresses of Docker containers to mynetworks
so that Docker containers are legal to send mails via the postfix server on the host. (reference and more details)
要在容器中使用sendmail,请安装ssmtp并将 FromLineOverride
设置为允许,而 mailhub
到 /etc/ssmtp/ssmtp.conf
中主机的IP地址。您可以将 mailhub
设置为诸如 smtp-server
的符号,然后运行带有 - add-host
选项,如(运行 - add-host smtp-server:your-docker0-address
)。这将在容器中配置可用的sendmail,这些容器实际上将使用主机MTA发送邮件。
To use sendmail in containers, install ssmtp and set FromLineOverride
to be permitted and and mailhub
to the IP address of the host in /etc/ssmtp/ssmtp.conf
. You can set mailhub
to be a symbol such as smtp-server
and then run the container with --add-host
option, as shown in this Dockerfile (run it with --add-host smtp-server:your-docker0-address
). This would configure a usable sendmail in containers which would actually use the host MTA to send mails.
这篇关于在docker容器内配置sendmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!