Dockerfile for postfix

dockerpostfix

I have tried to implement postfix in dockerfile, and it failed when i tried to send a mail saying (address resolved failure). Do i need to reconfigure the docker network or just the postfix main configuration?

Sending Email:

sendmail to@email.com
From: from@email.com
Subject: theSubject
This is the message
.

Output error:

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

postfix main configuration (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@gmail.com: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

Any help will be greatly appreciated.

Best Answer

You can bring the container our of the docker bridge network and instead use the host's network by adding --network="host" as an option to docker run ..... instead of mapping ports with the -p argument.

For docker-compose you can add network_mode: "host" instead of ports: & networks: for that specific service.

As an example when I run PHP containers on my local/dev environments.

Docker CLI

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

Docker Compose:

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