Configure sendmail inside a docker container

2019-02-03 04:02发布

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 abc.pqr@domain.com -t abc.pqr@domain.com

10条回答
我想做一个坏孩纸
2楼-- · 2019-02-03 04:34

EDIT: Please see xuhdev answer for more info and how to set up mail forwarder. My answer can be used to setup sendmail instead of postfix on host.

EDIT #2: Add firewall rule to allow smtp traffic from docker

I have made a similiar setup as the tarun mittal as following:

  • in docker app I have set it up to use the smtp server with the ip of docker0 interface (172.17.42.1)
  • in docker host, modified the /etc/mail/sendmail.mc to include listening on the docker0 interface (in contrast to all interfaces in tarun's answer - bold is added line)

    DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl

    DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=172.17.42.1')dnl

  • Allow the access from the docker containers to docker interface in firewall iptables -I INPUT -s 172.17.0.0/24 -d 172.17.42.1 -dport 25 -j ACCEPT

  • in /etc/mail/access I have added at the end to allow all docker instances to send emails and makemap hash /etc/mail/access < /etc/mail/access to compile database

    /// EDITED - USE BELOW AS DOCKER WILL GO TO 172.17.1.X after many rebuilds

    /// OLD - Connect:172.17.0 RELAY

    Connect:172.17 RELAY

  • Finally restart sendmail - service sendmail restart

查看更多
Anthone
3楼-- · 2019-02-03 04:35

Adding a fully qualified domain name to the Docker host name in /etc/hosts does the trick for me:

{YourDockerIP} {YouDockerHostName}.localdomain {YouDockerHostName}

To me it looks like this:

172.17.0.25 77f5a7ae8606.localdomain 77f5a7ae8606

You can also use this bash script to automatically update this line:

#!/bin/bash
line=$(head -n 1 /etc/hosts | awk '{printf "%s %s.localdomain %s", $1, $2, $2}')
sed -e "1 s/^.*$/${line}/g" /etc/hosts > hosts
# with sed -i, it actually performs a rename of /etc/hosts, but docker does not
# allow that, so we have to use a temp file and copy it to overwrite /etc/hosts
cp hosts /etc/hosts
rm hosts

Reference: http://hjk41.azurewebsites.net/2015/09/25/using-sendmail-inside-docker/

查看更多
Juvenile、少年°
4楼-- · 2019-02-03 04:36

You have to point inet_interfaces to docker bridge (docker0) in post fix config located at set /etc/postfix/main.cf

inet_interfaces =

More internal working detail at sending email from docker via postfix installed on host

Note: use ifconfig command to get the docker bridge address

查看更多
放荡不羁爱自由
5楼-- · 2019-02-03 04:36

According to the Sendmail manual we may use sendmail -bd command to run a service. But it exists with zero status. So...

# In your Dockerfile
RUN yum install -y sendmail
# Run mail listener
RUN sendmail -bd

CMD sleep infinity
# or run your command

Also you may use supervisord. I use this config:

[program:sendmail]
command=/usr/sbin/sendmail -bd
autostart=true
autorestart=false
numprocs=1
startretries=0

And test it:

$ echo test123 | sendmail your@mail.com

Don't forget to check Spam folder.

查看更多
对你真心纯属浪费
6楼-- · 2019-02-03 04:38

Nowhere in your Dockerfile is sendmail (or any other mail agent) installed. The host, however, apparently does have sendmail available. The "best" or most Docker-like solution is to spin up another container that runs an MTA (like postfix or exim), and configure your application to use that.

查看更多
疯言疯语
7楼-- · 2019-02-03 04:40

building on previous answers,
create config/sendmail_config.sh with:

#!/bin/sh
# set host in hosts
line=$(head -n 1 /etc/hosts)
line2=$(echo $line | awk '{print $2}')
echo "$line $line2.localdomain" >> /etc/hosts

yum install -y sendmail sendmail-cf m4 \
    && hostname >> /etc/mail/relay-domains \
    && m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

#remove localhost limit
sed -i -e "s/Port=smtp,Addr=127.0.0.1, Name=MTA/Port=smtp, Name=MTA/g" \
    /etc/mail/sendmail.mc
sendmail -bd

change yum for apt-get on debian based containers

then in Dockerfile add:

RUN sed -i -e "s#;sendmail_path =#sendmail_path = /usr/sbin/sendmail -t -i#g"  \
    /your_path_to/php.ini
COPY ./config/sendmail_config.sh .

I want sendmail with my php util so I can stick it anywhere with out having to link to another MTA container or host to complete the task.
I run sh sendmail_config.sh, and then run my php util.

查看更多
登录 后发表回答