Sending email from docker through Postfix installed on the host

We use MUP to deploy our Meteor app, NYBR. MUP creates three docker containers for running the meteor app, nginx reverse proxy and mongo. Like most applications, the need for sending the email came, ideally we should be using Amazon Web Services SES,  but getting their approval was a pain in the @$$ and was taking time. So, we did not want to hold back our development and decided to send the email through our own SMTP server until SES was approved.

We installed postfix and were hoping to get to developing and sending beautiful emails to our users. Unfortunately, we were greeted with the Connection refused error.

After trying various things in postfix configuration at,/etc/postfix/main.cf turns out there is something called docker bridge (docker0) which acts a bridge between your ethernet port and Docker containers so that data can go back and forth.

So you have to listen on the docker0 interface. To do that, type ifconfig on your host system to find out the bridge address and set your postfix to listen on it.

Docker Bridge address

As you can see in the image above, the IP is 172.17.0.1

In postfix configuration located at /etc/postfix/main.cf set

inet_interfaces = 172.17.0.1

While you are there, add your actual docker container ip address to mynetworks

mynetworks = 172.17.0.3 172.17.0.5

above two are private ip addresses of my docker containers from which I wish to send email.

You can find the IP address of your docker container by

docker inspect [container_id, container_name]

example

docker inspect my_container_name

Let me know in comments if you would like to learn how to send html emails in meteor.

11 Replies to “Sending email from docker through Postfix installed on the host”

  1. So, now I know how to configure my host’s postfix installation. But do I need to install something inside the container, like postfix? Let say I run an ubuntu:latest container. I don’t have sendmail installed, so I install postfix (which provides its own sendmail implementation). But then how do I configure the container’s postfix installation?

    I have quite a hard time understanding how all this works ^^

    1. You don’t have to install or configure containers postfix or sendmail. The container asks the host to send an email, and we have configured postfix to accept requests from the container.

      1. Really helpful and simple post…but I seem to be missing something, any hints would be appreciated. Do I need to start up the container in a particular way for it to use the host’s postfix configuration?

        I currently get “bash: mail: command not found” if I try to send an email via the container.

      2. How do you send email with postfix via container instance? I have tried using centos 7 image, however postfix does not start.

  2. I set this up and restarted postfix but inside the container, I’m still getting connection refused in the mail log. I also tried mapping port 25 inside the container but the container wouldn’t bind to 0.0.0.0:25 because postfix was already bound to that port.

  3. Hi,
    Thanks for nice blog.

    Right Now i’m looking to run the mailx sendmail feature from container. So I have build a base python image , where i have installed the mailx, postfix . When I run the docker image and after the access the running container.

    Inside container when I . run the command `echo “something” | mailx -v -s “subject” “xyz570@gmail.com”`

    Its shown message like : “Mail Delivery Status Report will be mailed to {username}”

    but did not any recieve any email on ‘xyz570@gmail.com.’

    Any help on that.
    Thanks in advance.

  4. no doesnt seem to work for me ,do i need to copy main.cf or install postfix inside container? my host in centos based and my container is ubuntu

  5. I installed mailutils on the container and that resolves the issue with the mail command not found. Now need to figure out how to get the container to use the Postfix configuration of the host…

  6. This works somewhat, but it also leads to my postfix server not starting while docker service is not up – on reboots it fails to start and /var/log/mail.err gives the output:

    fatal: parameter inet_interfaces: no local interface found for 172.17.0.1

Leave a Reply

Your email address will not be published. Required fields are marked *