Docker: how to use postfix from host


I’ve installed Nextcloud using the official docker image.
Now I want to use the postfix from the host to relay the mails from the docker container. I’ve used this guide to change configuration to expose postfix to the Docker network, but so far I haven’t been able to send mails from Nextcloud (fyi: It is working on the host).

This is what I’ve done:


mynetworks = [::ffff:]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces =
inet_protocols = ipv4

# specify SMTP relay host
relayhost =
# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# change sender mail address
sender_canonical_maps = regexp:/etc/postfix/sender_canonical

inet_interfaces = the IP address of docker0 interface using command ifconfig
mynetworks = : the subnet used by my Nextcloud docker container

Here are the mail settings in config.php from Nextcloud:

  'mail_domain' => '',
  'mail_from_address' => '',
  'mail_smtpmode' => 'smtp',
  'mail_sendmailmode' => 'smtp',
  'mail_smtpport' => '25',
  'mail_smtphost' => '',

I’m having 2 questions:

  1. which IP should I use for mail_smtphost ? I’ve tried with localhost, private IP of nextcloud container, private IP of the host…

  2. Should I expose port 25 from the host to the container (e.g. docker run -p 25:25 )?

Any help is appreciated


FYI:I’ve also opened port 25 on the host in iptables

it doesn’t work this way -p 25:25 maps port 25 of the container to the port 25 of the host - which obviously doesn’t work as it is used already (most likely your container doesn’t start with such setting)

localhost and private IP/fqdn of the container are the same (settings are applied to the the container, so all IPs and hostnames are from the container point of view). the right setting would be IP/fqdn of the host. According to the tutorial you referenced:

Instead of providing the known SMTP server’s IP and host, one should use the IP of docker0, as explained above. In the case of many nodes in Kubernetes cluster with different docker0 IP, the Docker container of Jenkins master should reside only on one host and docker0’s IP on that host should be used.

you need to your 172.x.x.x address of the docker0 interface (previously configure postfix to listen on this interface as well) - see "Modified “/etc/postfix/” in your tutorial

inet_interfaces = localhost,

I remember there are restrictions in place when docker container talks to the host but don’t have any reference now… try searching for "docker container access host ip"or similar…

Indeed, I totally overlooked this. Oddly, I tried it before and my container did start without any warning that the port was already in use.

After some testing using nmap in the container I figured out I could use the IP address of docker0 to send mail (Note: you can also use the private IP of the host but for this you first need to add this IP to the inet_interfaces in - but it doesn’t make any difference)

Anyway I can now send test mails from my Nextcloud instance.
However: when sharing a file or adding a note I got the following error in mail.log:

Apr  8 15:47:28  postfix/smtpd[3227107]: connect from unknown[]
Apr  8 15:47:28  postfix/smtpd[3227107]: 24BACFD9B4: client=unknown[]
Apr  8 15:47:28  postfix/cleanup[3227111]: 24BACFD9B4: message-id=<>
Apr  8 15:47:28  postfix/qmgr[3227061]: 24BACFD9B4: from=<>, size=20673, nrcpt=1 (queue active)
Apr  8 15:47:28  postfix/smtpd[3227107]: disconnect from unknown[] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Apr  8 15:47:30  postfix/smtp[3227112]: 24BACFD9B4: to=<>,[]:587, delay=2.2, delays=0.01/0.02/1.9/0.32, dsn=5.0.0, status=bounced (host[] said: 550 Request failed; Mailbox unavailable (in reply to end of DATA command))
Apr  8 15:47:30  postfix/cleanup[3227111]: 5DFB9FD9B6: message-id=<>
Apr  8 15:47:30  postfix/bounce[3227113]: 24BACFD9B4: sender non-delivery notification: 5DFB9FD9B6
Apr  8 15:47:30  postfix/qmgr[3227061]: 5DFB9FD9B6: from=<>, size=22894, nrcpt=1 (queue active)
Apr  8 15:47:30  postfix/qmgr[3227061]: 24BACFD9B4: removed

(FYI: is the IP of the nextcloud container)

This is very similar to the error I got last year using the same mail provider (yahoo) but using SMTP directly to yahoo.

It does seems strange to my as an 550 error means that the mailbox of the recipient does not exist, but this is not the case here obviously. How can I solve this, or should I create a separate post for this?

I have no idea about postfix but the following log

looks for me like you postfix accepted the message and forwarded it to yahoo, which in turn rejected the message (most likely to avoid open relay). I assume your postfix tried to send the message to yahoo without authentication…

According to this post - yahoo doesn’t like certain Reply To headers.

I’ve compared my mail headers, see below:

Test mail (=OK)

Received: from (unknown [])
	by (Postfix) with ESMTP id 008BAFD996
	for <>; Fri,  8 Apr 2022 16:19:29 +0200 (CEST)
Message-ID: <>
Subject: Mail test
From: My Cloud <>
To: admin <>
MIME-Version: 1.0
Content-Type: multipart/alternative;
References: <>
X-Mailer: WebService/1.1.20001
Content-Length: 15170

Share by mail (bounced with 550 error)

Return-Path: <>
Received: from (unknown [])
	by (Postfix) with ESMTP id 24BACFD9B4
	for <>; Fri,  8 Apr 2022 15:47:28 +0200 (CEST)
Message-ID: <>
Date: Fri, 08 Apr 2022 13:47:28 +0000
Subject: "admin" voegde een notitie toe aan een bestand dat met jou is
From: admin via My Cloud <>
Reply-To: admin <>
MIME-Version: 1.0
Content-Type: multipart/alternative;

In the failed mail I do see a Reply-To mailadres which is different than the mail that the mail is send from. Maybe this causes the 550 mailbox unavailable error?
If so, this could be possibly solved by adding a fixed Reply-To header with the nextcloud mailadres (in my example
(I don’t know whether this is possible but I assume it is)

I’ve checked with my other Nextcloud instance which is installed natively and also uses postfix.
When sending a mail by share by mail I notice that the Reply-To is indeed the mailadres used by Nextcloud to send the mails (instead of the mailadres of the user sharing the file. Thus guess the solution is indeed to somehow change the Reply-To headers?

Date: Fri, 08 Apr 2022 18:26:57 +0000
Subject: share by mail
From: user via Other-Cloud <>
Reply-To: user <>
MIME-Version: 1.0
Content-Type: multipart/alternative;
References: <>
X-Mailer: WebService/1.1.20001
Content-Length: 19904

Edit 2:
Seems that I was right. I used this to replace my Reply-To headers in my mail with the Nextcloud-mail address & now I don’t receive any 550 error anymore but 250 OK :slight_smile:
Only downside is that the mails are delivered to the spam box from the recipient, but guess this is due to the nature of the automatically send mail.

I’ll send another post with the full solution of my 2 problems, guess it can help others who are using free mail services.


OK so here is what I’ve done to make postfix listen to my docker container:
add/change the following lines to /etc/postfix/main.cfg:

  • mynetworks = add the IP address range of your nextcloud docker container e.g.

Tip: add the folowing to your docker-compose.yml to add a static IP address subnet to your docker container:

       - subnet:
  • inet_interfaces = add the IP address of your docker0 interface e.g.
    (user ip addr show of ifconfig and note the IP address corresponding to docker0, you don’t need to enter the mask e.g. /16)

Then restart your postfix service (e.g. sudo service postfix restart). If you have a firewall like ufw or iptables active, make sure you add rules to accept connections from your Nextcloud container IP to port 25.

Now you can add add the following to your Nextcloud config.php:

  'mail_domain' => '',
  'mail_from_address' => '',
  'mail_smtpmode' => 'smtp',
  'mail_sendmailmode' => 'smtp',
  'mail_smtpport' => '25',
  'mail_smtphost' => '', //the IP address of docker0

For server address use the IP address of your docker0 interface and all should work